MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

मोंगोडब अपडेट पुश ऐरे

कोई है जो तत्व को एक सरणी में धकेलने की कोशिश कर रहा है, अब देशी मोंगोडब पुस्तकालय का उपयोग कर संभव है।

निम्नलिखित मोंगोडब संग्रह वस्तु को ध्यान में रखते हुए

{ 
"_id" : 5,
"attachments": [
    { 
        "id": "xxxxxxx",
        "subtype": "book",
        "title": "xxxx",
        "body": "xxxx" ,
        "filetype" : "xxxxx"
    },
    {
        "id": "xxxxxxx",
        "subtype": "book",
        "title": "xxxx",
        "body": "xxxx",
        "filetype": "xxxxx"
    }
]
}


 arr = [{
 'id':'123456',
 'subtype':'book',
 'title'  : 'c programing',
 'body'  :' complete tutorial for c',
 'filetype' : '.pdf'
 },
{
 'id':'123457',
 'subtype':'book',
 'title'  : 'Java programing',
 'body'  :' complete tutorial for Java',
 'filetype' : '.pdf'
 }
];

निम्नलिखित क्वेरी का उपयोग सरणी तत्व को अंत में "अटैचमेंट" में धकेलने के लिए किया जा सकता है। इसके लिए $push या $addToSet का उपयोग किया जा सकता है।

यह अनुलग्नकों में एक वस्तु या तत्व सम्मिलित करेगा

db.collection('books').updateOne(
  { "_id": refid }, // query matching , refId should be "ObjectId" type
  { $push: { "attachments": arr[0] } } //single object will be pushed to attachemnts
 ).done(function (err, updElem) {
  console.log("updElem" + JSON.stringify(updElem));     
});

यह ऐरे में प्रत्येक ऑब्जेक्ट को अटैचमेंट में सम्मिलित करेगा

   db.collection('books').updateOne(
     { "_id": refid }, // query matching , refId should be "ObjectId" type
     { $push: { "attachments":{$each: arr} } } // arr will be array of objects
     ).done(function (err, updElem) {
           console.log("updElem" + JSON.stringify(updElem));     
    });


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. mongodb 4.0 . से डुप्लीकेट रिकॉर्ड निकालें

  2. Nodejs में विशिष्ट Mongodb का उपयोग नहीं कर सकता

  3. इमोजी मोंगोडब में सही ढंग से सहेजा नहीं गया है

  4. मोंगोडब में नेस्टेड सरणी में डेटा सम्मिलित करना

  5. mongoDB नक्शा / घटाएँ घटाएँ