Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 367
Gunjan Patel Avatar asked Aug 11 '18 14:08

Gunjan Patel


People also ask

How do I query multiple documents in MongoDB?

You can query for multiple documents in a collection with collection.find() . The find() method uses a query document that you provide to match the subset of the documents in the collection that match the query.

What is multi option in update in MongoDB?

By default, the db. collection. update() method updates a single document. Include the option multi: true to update all documents that match the query criteria.

How do I query multiple values in MongoDB?

MongoDB provides the find() that is used to find multiple values or documents from the collection. The find() method returns a cursor of the result set and prints all the documents. To find the multiple values, we can use the aggregation operations that are provided by MongoDB itself.


1 Answers

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 }
      }
    })
  )
})
like image 129
Ashh Avatar answered Sep 21 '22 22:09

Ashh