Atomicty of the entire operation

30 views
Skip to first unread message

revit

unread,
Sep 7, 2016, 6:39:18 AM9/7/16
to mongodb-user
I ran the following scenario:

First, inserted documents:
for (var i = 0; i <100; i++) {db.col1.insert({_id: i, "new5" :22})}

Then updated them with the following command:
db.col1.updateMany({}, {$set: {  "new5": 900, "new6" : 600  }  })

The server was killed while the updateMany command was running and after it was up again I checked the status of the collection.
I saw that some of documents were with the updated value "new5" : 900, "new6" : 600 and other remained with the original value although I expected a rollback will be applied and all the documents
will be retrieved to the old value. Am I missing something? is there a special mode which guarantees the atomicty of the entire update operation? Thanks!

John Murphy

unread,
Sep 14, 2016, 1:55:04 AM9/14/16
to mongodb-user

Hi revit,

In MongoDB, a write operation is atomic on the level of a single document. Operations that involve multiple documents are not atomic.

For cases where a sequence of write operations must operate as if in a single transaction, you can follow our manual on how to perform a two-phase commit in your application.

Regards,
John Murphy

Reply all
Reply to author
Forward
0 new messages