Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does findAndModify effectively lock the document to prevent update conflicts?

What type of locking does findAndModify() offer? Is is a write lock only, or read/write? Does it prevent simultaneous updates on the same record?

like image 629
user646584 Avatar asked Feb 23 '26 15:02

user646584


1 Answers

MongoDB has a global (per-instance) write lock, which serializes all updates across all data in the server (though different servers in a sharded cluster will each have their own independent locks). This means that at any given instant in time, only one update is taking place on any document, and therefore only one update for any given document.

findAndModify doesn't do anything different in this regard than an ordinary update -- it just returns the document to you.

like image 61
dcrosta Avatar answered Feb 25 '26 10:02

dcrosta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!