3

I want to insert array in already exist documents .

now my document look like this:

{
    "_id" : ObjectId("5604f0150fe136e9292ee16a"),
    "name" : "mamy",
    "url_Address" : "http://www.flipkart.com/mamy-poko-pants-diaper-large/p/itmdbdffn8gjzpfz?pid=DPRDADE2Z8BZGYZG&ref=L%3A-2196533682561335257&srno=p_1&query=mamy&otracker=from-search",
    "vendor_Name" : "WS Retail",
    "vendor_rating" : "4.2 / 5",
    "last_price_1" : "Rs. 699",
    "last_price_2" : "Rs. 699",
    "prce" : "Rs. 699",
    "product_Name" : "Mamy Poko Pants Diaper - Large",
    "MRP" : "Rs 573"
}

and i want to add an array in this with name competitor :

{
    "_id" : ObjectId("5604f0150fe136e9292ee16a"),
    "name" : "mamy",
    "url_Address" : "http://www.flipkart.com/mamy-poko-pants-diaper-large/p/itmdbdffn8gjzpfz?pid=DPRDADE2Z8BZGYZG&ref=L%3A-2196533682561335257&srno=p_1&query=mamy&otracker=from-search",
    "vendor_Name" : "WS Retail",
    "vendor_rating" : "4.2 / 5",
    "last_price_1" : "Rs. 699",
    "last_price_2" : "Rs. 699",
    "prce" : "Rs. 699",
    "product_Name" : "Mamy Poko Pants Diaper - Large",
    "MRP" : "Rs 573",
    "Competitor : [{
                     "cat_id" : "xx",
                     "name" : "mamy",
                     "url_Address" : "http://www.flipkart.com/mamy-poko-pants-diaper-large/p/itmdbdffn8gjzpfz?pid=DPRDADE2Z8BZGYZG&ref=L%3A-2196533682561335257&srno=p_1&query=mamy&otracker=from-search",
                     "vendor_Name" : "WS Retail",
                     "vendor_rating" : "4.2 / 5",
                     "last_price_1" : "Rs. 699",
                     "last_price_2" : "Rs. 699",
                     "prce" : "Rs. 699",
                     "product_Name" : "Mamy Poko Pants Diaper - Large",
                     "MRP" : "Rs 573"
                    },
                  {
                     "cat_id" : "xxx",
                     "name" : "mamy",
                      "url_Address" : "http://www.flipkart.com/mamy-poko-pants-diaper-large/p/itmdbdffn8gjzpfz?pid=DPRDADE2Z8BZGYZG&ref=L%3A-2196533682561335257&srno=p_1&query=mamy&otracker=from-search",
                     "vendor_Name" : "WS Retail",
                     "vendor_rating" : "4.2 / 5",
                     "last_price_1" : "Rs. 699",
                     "last_price_2" : "Rs. 699",
                     "prce" : "Rs. 699",
                     "product_Name" : "Mamy Poko Pants Diaper - Large",
                     "MRP" : "Rs 573"
                   }]"
}

what query i have to write in mongodb ?

what query i have to write in Nodejs ?

1

3 Answers 3

0

You can add whole Competitor array in existing document with update given in doc

The query will be as:

db.collection.update({
    "_id": ObjectId("5604f0150fe136e9292ee16a")
}, {
    $set: {
    "Competitor": [{
        "cat_id": "xx",
        "name": "mamy",
        "url_Address": "http://www.flipkart.com/mamy-poko-pants-diaper-large/p/itmdbdffn8gjzpfz?pid=DPRDADE2Z8BZGYZG&ref=L%3A-2196533682561335257&srno=p_1&query=mamy&otracker=from-search",
        "vendor_Name": "WS Retail",
        "vendor_rating": "4.2 / 5",
        "last_price_1": "Rs. 699",
        "last_price_2": "Rs. 699",
        "prce": "Rs. 699",
        "product_Name": "Mamy Poko Pants Diaper - Large",
        "MRP": "Rs 573"
    }, {
        "cat_id": "xxx",
        "name": "mamy",
        "url_Address": "http://www.flipkart.com/mamy-poko-pants-diaper-large/p/itmdbdffn8gjzpfz?pid=DPRDADE2Z8BZGYZG&ref=L%3A-2196533682561335257&srno=p_1&query=mamy&otracker=from-search",
        "vendor_Name": "WS Retail",
        "vendor_rating": "4.2 / 5",
        "last_price_1": "Rs. 699",
        "last_price_2": "Rs. 699",
        "prce": "Rs. 699",
        "product_Name": "Mamy Poko Pants Diaper - Large",
        "MRP": "Rs 573"
    }]
    }
})
Sign up to request clarification or add additional context in comments.

Comments

0

You need to replace document to add fields:

var updateRestaurants = function(db, callback) {
   db.collection('restaurants').replaceOne(
      { "restaurant_id" : "41704620" },
      {
        "name" : "Vella 2",
        "address" : {
           "coord" : [ -73.9557413, 40.7720266 ],
           "building" : "1480",
           "street" : "2 Avenue",
           "zipcode" : "10075"
        }
      },
      function(err, results) {
        console.log(results);
        callback();
   });
};

look into documentation: http://docs.mongodb.org/getting-started/node/update/

The replacement document can have different fields from the original document.

but be care

After the update, the document only contains the field or fields in the replacement document.

So you need to get document, add you new fields and replace all the document

Comments

0

You can use $set operator This is official docs

so, what you have to do is

db.collection('collectionName').updateOne({ _id: ObjectId("5604f0150fe136e9292ee16a") }, { $set: { "Competitor": value_you_want_to_insert } })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.