简体   繁体   English

如何使用nodejs在已经存在的文档中添加数组?

[英]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"
    }]
    }
})

You can use $set operator This is official docs您可以使用$set operator 这是官方文档

so, what you have to do is所以,你要做的是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript 将项目添加到数组中,如果它不存在 - JavaScript Add item to array if its not already exist Javascript,如何将 object 添加到数组中,如果数组中不存在此 object 的某些属性 - Javascript, how to add an object to an array, if some properties of this object dont already exist in the array 如何检查数组中是否已存在自定义对象? - How to check if already custom object exist in array? 如何验证数组值是否已经存在 - How to validate array value already exist or not PHP - 如何检查数组中是否已存在值? - PHP - How to check if value already exist in array? Java:仅当其中一个元素不存在时,如何将JSON对象添加到JSON数组 - Java: How to add a JSON object to a JSON array only if one of the elements don't already exist 如何查找数组nodejs中是否不存在字符串 - how to find if string not exist in array nodejs 使用nodejs在mongodb中更新文档中的数组字段 - update an array field in the document in mongodb using nodejs 使用javascript,只要该项目不存在于数组中就推送到数组 - push to array as long as the item does not already exist in array using javascript 如何编写 if exists 语句块以检查 Powershell 模块是否已使用名称数组安装 - How to write if exist statement block to check if Powershell Modules are already installed using array of names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM