Home / Companies / MongoDB / Blog / Post Details
Content Deep Dive

How To SELECT ... FOR UPDATE inside MongoDB Transactions

Blog post from MongoDB

Post Details
Company
Date Published
Author
Renato Riccio
Word Count
1,309
Language
English
Hacker News Points
1
Summary

SELECT ... FOR UPDATE inside MongoDB Transactions` allows locking rows being read as though they were updated, preventing other operations from modifying or deleting them until the transaction ends. This behavior can be replicated in MongoDB by updating a document with an ObjectId value that changes its current value, ensuring writeConflicts when another operation tries to modify it. The `findOneAndUpdate` method is used to update a locking field in the document and return the document to the application in one round trip, keeping consistency without extra steps.