GJain
GJain

Reputation: 5093

is mongo update ($set) on a single document atomic

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??

Upvotes: 7

Views: 5558

Answers (2)

JohnnyHK
JohnnyHK

Reputation: 311835

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.

Upvotes: 10

turtlemonvh
turtlemonvh

Reputation: 9759

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.

Upvotes: 5

Related Questions