[英]how to add array in already exist document using nodejs?
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?我必须在 mongodb 中写什么查询?
what query i have to write in Nodejs?我必须在 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/ 查看文档: 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 您可以在现有文档中添加整个
Competitor
数组,并在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"
}]
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.