我想在已存在的文档中插入数组。
现在我的文档看起来像这样:
{
"_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中写什么查询?
答案 0 :(得分:0)
您需要替换文档以添加字段:
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();
});
};
查看文档:{{3}}
替换文档可以包含与原始文档不同的字段 文档。
但要小心
更新后,文档仅包含字段 替换文件。
所以你需要获取文档,添加新字段并替换所有文档
答案 1 :(得分:0)
您可以在现有文档中添加整个Competitor
数组,并在doc
查询将如下:
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"
}]
}
})