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?
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
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"
}]
}
})
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 } })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.