1

In mongodb, a write operation that updates multiple documents is not atomic.If there are 3 record A,B,C need to be updated.

Does mongodb:

find A, update A, find B, update B, find C, update C

or

find A,B,C and save to memory, forEach, update A,B,C?

王如锵
  • 941
  • 7
  • 17

1 Answers1

0

Bulk Update is supported in MongoDB.

Please see this example

db.mycollection.find()
{ "_id" : 1, "name" : "A", "designation" : "developer", "company" : "XYZ" }
{ "_id" : 2, "name" : "B", "designation" : "developer", "company" : "XYZ", "expe
rience" : 2 }
{ "_id" : 3, "name" : "C", "designation" : "developer", "company" : "XYZ", "expe
rience" : 3 }
{ "_id" : 4, "name" : "D", "designation" : "developer", "company" : "XYZ", "expe
rience" : 5 }
{ "_id" : 5, "name" : "E", "designation" : "Manager", "company" : "XYZ", "experi
ence" : 10 }
{ "_id" : 6, "name" : "F", "designation" : "Manager", "company" : "XYZ", "experi
ence" : 10 }
{ "_id" : 7, "name" : "G", "designation" : "Manager", "company" : "ABC", "experi
ence" : 10 }
{ "_id" : 8, "name" : "H", "designation" : "Developer", "company" : "ABC", "expe
rience" : 5 }
{ "_id" : 9, "name" : "I", "designation" : "Developer", "company" : "XYZ", "expe
rience" : 5 }

I'm going to do bulk update on mycollection based on the below criteria

XYZ company giving promotion to their members who are developers who are having an experience of 5 years to senior developer

My commands in Mongo shell are

var bulk = db.mycollection.initializeUnorderedBulkOp();

bulk.find({ $and:[{company:"XYZ"}, {experience:5}] }).update({$set:{designation:"
senior developer", comments:"Congrats you have been promoted to senior developer
"}});

bulk.execute();

Mongo Shell we will get the below result after executing the query

BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 0,
        "nUpserted" : 0,
        "nMatched" : 2,
        "nModified" : 2,
        "nRemoved" : 0,
        "upserted" : [ ]
})

Two documents where matching our condition of company:"XYZ" and experience:5 and those documents were updated (_id:4, _id:9)

db.mycollection.find()

{ "_id" : 1, "name" : "A", "designation" : "developer", "company" : "XYZ" }
{ "_id" : 2, "name" : "B", "designation" : "developer", "company" : "XYZ", "expe
rience" : 2 }
{ "_id" : 3, "name" : "C", "designation" : "developer", "company" : "XYZ", "expe
rience" : 3 }
{ "_id" : 4, "name" : "D", "designation" : "senior developer", "company" : "XYZ"
, "experience" : 5, "comments" : "Congrats you have been promoted to senior deve
loper" }
{ "_id" : 5, "name" : "E", "designation" : "Manager", "company" : "XYZ", "experi
ence" : 10 }
{ "_id" : 6, "name" : "F", "designation" : "Manager", "company" : "XYZ", "experi
ence" : 10 }
{ "_id" : 7, "name" : "G", "designation" : "Manager", "company" : "ABC", "experi
ence" : 10 }
{ "_id" : 8, "name" : "H", "designation" : "Developer", "company" : "ABC", "expe
rience" : 5 }
{ "_id" : 9, "name" : "I", "designation" : "senior developer", "company" : "XYZ"
, "experience" : 5, "comments" : "Congrats you have been promoted to senior deve
loper" }

Other related interesting References:

What's the difference between findAndModify and update in MongoDB?

https://docs.mongodb.com/manual/reference/method/Bulk.find.update/

https://docs.mongodb.com/manual/reference/method/db.collection.findAndModify/

https://docs.mongodb.com/v3.2/reference/method/db.collection.findOneAndUpdate/

Hope it Helps!!

Community
  • 1
  • 1
Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34