Is in Mongo any way to do check and setting like atomic operation ? I am making booking for hotels and if there is free room you can reserve, but what if two or more people want to reserve in same time. Is there anything similar to transaction in Mongo or any way to solve this problem ?
In MongoDB, a write operation is atomic on the level of a single document, even if the operation modifies multiple embedded documents within a single document.
As previously discussed, the only way to maintain atomicity in MongoDB is to keep all related information in a single document that is updated on a regular basis. A single document of this type can be created using embedded documents, as shown below.
Upsert is not atomic.
In MongoDB, insert operations target a single collection. All write operations in MongoDB are atomic on the level of a single document. For examples, see Insert Documents.
Yes, that's the classic use case for MongoDB's findAndModify
command.
Specifically for pymongo: find_and_modify
.
All updates are atomic operations over a document. Now find_and_modify locks that document and returns it back in the same operation. This allows you to combine a lock over the document during find and then applies the update operation.
You can find more about atomic operations: http://www.mongodb.org/display/DOCS/Atomic+Operations
Best,
Norberto
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With