Mongo - How to update multiple documents with different value in single query?

守給你的承諾、 提交于 2019-12-18 06:31:19

问题


I want to write a query for updating multiple documents in a single query, Please suggest me possible ways.

Following is my mongo document.

[
    {
        "_id" : ObjectId("5b0f0a2ca1f6633032c204cd"),
        "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"),
        "name" : "ABC",
        "userType": "admin"
    },
    {
        "_id" : ObjectId("5b0f0a2ca1f6633032c204ce"),
        "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"),
        "name" : "DEF",
        "userType": "admin"
    },
    {
        "_id" : ObjectId("5b0f0a2ca1f6633032c204cf"),
        "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"),
        "name" : "GHI",
        "userType": "admin"
    },
    {
        "_id" : ObjectId("5b0f0a2ca1f6633032c204d0"),
        "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"),
        "name" : "XYZ",
        "userType": "admin"
    }
]

I've following the updated object in code, how can I update that in a database with the query.

[
    {
        "_id" : ObjectId("5b0f0a2ca1f6633032c204cd"),
        "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"),
        "name" : "ABC123",
        "color":"red"
        "userType": "admin"
    },
    {
        "_id" : ObjectId("5b0f0a2ca1f6633032c204ce"),
        "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"),
        "name" : "DEF123",
        "color":"blue"
        "userType": "admin"
    },
    {
        "_id" : ObjectId("5b0f0a2ca1f6633032c204cf"),
        "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"),
        "name" : "GHI123",
        "color":"green"
        "userType": "admin"
    },
    {
        "_id" : ObjectId("5b0f0a2ca1f6633032c204d0"),
        "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"),
        "name" : "XYZ123",
        "color":"rgb(14,256,12, 1)"
        "userType": "admin"
    }
]

Please suggest me the proper ways, or We can do it or not?


回答1:


You basically need bulkWrite operation in mongodb

const array = [
  {
    "_id" : ObjectId("5b0f0a2ca1f6633032c204cd"),
    "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"),
    "name" : "ABC123",
    "color":"red"
    "userType": "admin"
  },
  {
    "_id" : ObjectId("5b0f0a2ca1f6633032c204ce"),
    "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"),
    "name" : "DEF123",
    "color":"blue"
    "userType": "admin"
  },
  {
    "_id" : ObjectId("5b0f0a2ca1f6633032c204cf"),
    "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"),
    "name" : "GHI123",
    "color":"green"
    "userType": "admin"
  },
  {
    "_id" : ObjectId("5b0f0a2ca1f6633032c204d0"),
    "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"),
    "name" : "XYZ123",
    "color":"rgb(14,256,12, 1)"
    "userType": "admin"
  }
]

And the final query

Model.bulkWrite(
  array.map((data) => 
    ({
      updateOne: {
        filter: { _id: data._id },
        update: { $set: data }
      }
    })
  )
})


来源:https://stackoverflow.com/questions/51800392/mongo-how-to-update-multiple-documents-with-different-value-in-single-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!