Does findAndModify really lock the document to prevent read conflicts?

Suppose there is a document structure as shown below:
{_id:"session-01", status:"free"}

And there are two parallel operations that perform the findAndModify operation below: db.collection.findAndModify({query:{status:"free"}, update:{status:"occupied"}, new:true})

What I want to achieve is that only one operation can get "free" and lock on it, so the other operation must be zero. Is this what findAndModify does?

+7
mongodb mongodb-query
source share
1 answer

According to official docs, MongoDB provides an isolated update and return.
Here is the link of the mongoDB whitepaper: Why findAndModify is Atomic .
Here's a superbly illustrated example of how findAndModify works in mongoDB: How findAndModify works

Note. I usually don’t like publishing links, but I didn’t want to discredit content owners.

+6
source share

All Articles