6

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?

5
  • What you have tried yet? Commented Aug 11, 2018 at 14:43
  • Nothing, I tried with updateMany and with upsert: true. Commented Aug 13, 2018 at 4:24
  • what type of array your are passing to update the documents? Update your question and post the payload array by which you want to update the multiple documents Commented Aug 13, 2018 at 5:26
  • From same as output object. just want to compare _id and update all data Commented Aug 13, 2018 at 6:48
  • Try my this answer as well stackoverflow.com/questions/51801033/… Commented Aug 13, 2018 at 17:28

1 Answer 1

13

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 }
      }
    })
  )
})
Sign up to request clarification or add additional context in comments.

2 Comments

@TBE For query OR for the above data in array?
For the query, you have an un-needed closing brace. Your last line shall be ) instead of })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.