Arrays 如何使用nodejs在已经存在的文档中添加数组?
我想在已经存在的文档中插入数组 现在,我的文档如下所示:Arrays 如何使用nodejs在已经存在的文档中添加数组?,arrays,node.js,mongodb,Arrays,Node.js,Mongodb,我想在已经存在的文档中插入数组 现在,我的文档如下所示: { "_id" : ObjectId("5604f0150fe136e9292ee16a"), "name" : "mamy", "url_Address" : "http://www.flipkart.com/mamy-poko-pants-diaper-large/p/itmdbdffn8gjzpfz?pid=DPRDADE2Z8BZGYZG&ref=L%3A-2196533682561335257&a
{
"_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的数组:
{
"_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"
}]"
}
我必须在mongodb中编写什么查询
我必须在Nodejs中编写什么查询?您需要替换文档以添加字段:
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();
});
};
查阅文件:
替换文档可以具有与原始文档不同的字段
文件
但是要小心
更新后,文档仅包含中的一个或多个字段
替换文件
因此,您需要获取文档,添加新字段并替换所有文档您可以在现有文档中添加整个
竞争对手
数组,并在中给出更新
查询内容如下:
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"
}]
}
})
以下是您对mongodb查询的答案