0

I am trying to figure out a way to update multiple documents in a collection with different values based on a key.

persons collection:

 {
  _id: 1,
  name: "Jackie Chan",
  Country: Australia
},
{
  _id: 2,
  name: "Brad Pitt",
  Country: Russia
},
{
  _id: 3,
  name: "Al Pacino",
  Country: USA
}

Payload:

{
 _id: 1,
 name:"Jackie Chan",
 Country:"China"
}
,{
 _id: 2,
 name:"Brad Pitt",
 Country:"USA"
}

persons collection after update:

{
      _id: 1,
      name: "Jackie Chan",
      Country: "China"
    },
    {
      _id: 2,
      name: "Brad Pitt",
      Country: "USA"
    },
    {
      _id: 3,
      name: "Al Pacino",
      Country: "USA"
    }

SQL equivalent would be :

update t1.country = t2.country from persons t1 inner join #temptable t2 on t1._id=t2._id

None of the examples mentioned here explain how to do it. Unless I am missing something?

7
  • Are you trying to do a bulk update? Commented Aug 28, 2020 at 9:03
  • Sort of yes. If you mean sending multiple documents to db for update at once then yes. @Joe Commented Aug 28, 2020 at 9:10
  • I'm not really sure how Brat Pitt/Russia + Brad Pitt/USA = Brad Pitt/China. Commented Aug 28, 2020 at 9:14
  • does this help: docs.mongodb.com/manual/core/bulk-write-operations/… Commented Aug 28, 2020 at 9:14
  • @Joe that was a mistake on my part while copy pasting. I have corrected it. According to the link that you have shared.. does it mean I will have to have multiple { updateOne :{}} within bulkWrite() . As many times as there are records to be updated ? Commented Aug 28, 2020 at 9:20

2 Answers 2

1

It seems like bulk write is exactly the right tool. Simply map the payload array so to make it an array of updates, such as:

db.persons.bulkWrite(payload.map( function(p) { 
      return { updateOne:{
                    filter: {_id: p._id},
                    update: {$set: {Country: p.Country}}
      }}
}))
Sign up to request clarification or add additional context in comments.

5 Comments

I am not able to get this to work with ObjectId, any ideas? await this.db.collection("persons").bulkWrite([{ _id: { "$toObjectId": "5f3258cfbaaccedaa5dd2d96" }, phone: "123456" } , { _id: { "$toObjectId": "5f3258cfbaaccedaa5dd2da2" }, phone: "1234567" }].map(function (p) { return { updateOne: { filter: { _id: p._id }, update: { $set: { phone: p.phone } } } } }))
ah got it.. i used new mongo.ObjectID("5f3258cfbaaccedaa5dd2da2") and it worked
I have a question though. I got this in response: { "acknowledged" : true, "deletedCount" : 0, "insertedCount" : 0, "matchedCount" : 2, "upsertedCount" : 0, "insertedIds" : { }, "upsertedIds" : { } } Why is it that it says that 0 records were modified?
The existing document already had the value that the update operation was trying to set.
what happens if one insert fails?
0
//code when run from mongodb client
> db.persons.find();
{ "_id" : 1, "name" : "Jackie Chan", "Country" : "China1" }
{ "_id" : 2, "name" : "Brad Pitt", "Country" : "Russia1" }
{ "_id" : 3, "name" : "Al Pacino", "Country" : "USA1" }
> var payload=[{_id: 1,name:"Jackie Chan",Country:"China2"},
...              {_id: 2,name: "Brad Pitt",Country: "Russia2"},
...              {_id: 3, name: "Al Pacino",Country: "USA2"}];
> print("payload: ",payload);
payload:  [object Object],[object Object],[object Object]
>
> db.persons.bulkWrite(payload.map( function(p) {
...       return { updateOne:{
...                     filter: {_id: p._id},
...                     update: {$set: {Country: p.Country}}
...       }}
... }));
{
        "acknowledged" : true,
        "deletedCount" : 0,
        "insertedCount" : 0,
        "matchedCount" : 3,
        "upsertedCount" : 0,
        "insertedIds" : {

        },
        "upsertedIds" : {

        }
}
> db.persons.find();
{ "_id" : 1, "name" : "Jackie Chan", "Country" : "China2" }
{ "_id" : 2, "name" : "Brad Pitt", "Country" : "Russia2" }
{ "_id" : 3, "name" : "Al Pacino", "Country" : "USA2" }
>

Comments

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.