6

I have a code similar to

db.myColletion.update({'_id':ObjectId("...")}, {'$set': {'state': 'CA'}})

Is above an atomic operation?

Do I need to use findAndModify even on single document for atomicity??

GJain
  • 5,025
  • 6
  • 48
  • 82
  • 1
    possible duplicate of [what's the diff between findAndModify and update in MongoDB?](http://stackoverflow.com/questions/10778493/whats-the-diff-between-findandmodify-and-update-in-mongodb) – pennstatephil May 31 '14 at 21:04

2 Answers2

10

Yes, all write operations with MongoDB are atomic at the level of a single document.

The key difference between update and findAnyModify is that the latter also provides you with the original or updated document.

Community
  • 1
  • 1
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
5

See the docs for findAndModify, specifically the section on comparisons with the update method.

When modifying a single document, both findAndModify and the update() method atomically update the document.

turtlemonvh
  • 9,149
  • 6
  • 47
  • 53