Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Mongo any way to do check and setting like atomic operation?

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 ?

like image 350
Damir Avatar asked Aug 16 '12 15:08

Damir


People also ask

Is MongoDB set atomic?

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.

How do you ensure atomicity in MongoDB?

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.

Is MongoDB Upsert Atomic?

Upsert is not atomic.

At what level are all the insert operations in MongoDB 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.


2 Answers

Yes, that's the classic use case for MongoDB's findAndModify command.

Specifically for pymongo: find_and_modify.

like image 121
JohnnyHK Avatar answered Oct 07 '22 17:10

JohnnyHK


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

like image 36
norberto Avatar answered Oct 07 '22 17:10

norberto