Assume there is document's structure like below:
{_id:"session-01", status:"free"}
And there are 2 concurrent operations that perform the findAndModify
operation below:
db.collection.findAndModify({query:{status:"free"}, update:{status:"occupied"}, new:true})
What I want to achieve is only one operation can get the "free" one, and perform lock on it, so the other operation must be getting null. Is that what the findAndModify does?
According to official docs, MongoDB does provide isolated update and return.
Here is the mongoDB official doc link: Why findAndModify is Atomic.
Here is a superbly illustrated example of how findAndModify works in mongoDB : How findAndModify works
Note - I usually do not enjoy posting links but I did not want to discredit the content owners.