Question

i know, there is no transaction support on mongo DB. But now i need to read an value of an document, increment by 1 and write the new value.

Or - different way: Update an element and read the value at the same time. For this i like to use find and modify : http://www.mongodb.org/display/DOCS/findAndModify+Command

this command updates an document and returns the value before updating. Is this happens in on (same like) transaction? The point is: is it possible that an other session updates the same value between the two steps of the other session?

(sorry, for me it's hard to explain - i hope you understand what i'm trying to say)

thank you.

Was it helpful?

Solution

findAndModify command is atomic. Which means that no other operation can happen between find and modify parts. This command was made exactly for things like incrementing counters/settings flags and reading them at the same time.

OTHER TIPS

I still doubt about the atomicity.

Under "Upsert and Unique Index¶" Section here http://docs.mongodb.org/manual/reference/command/findAndModify/

It is written that multiple clients can perform query operation simultaneously, and then they will perform update operation.

When the findAndModify command includes the upsert: true option and the query field(s) is not uniquely indexed, the command could insert a document multiple times in certain circumstances.

If all the commands finish the query phase before any command starts the modify phase, and there is no unique index on the name field, the commands may each perform an upsert, creating multiple duplicate documents.

If it would have been atomic then it should assume a read write lock for time being thus preventing other connections from performing "query phase".

Is it a case with only upsert flag ? Since it creates a new document, may be it would be okay when a document match is found and it behaves Atomic in nature in that case. Can someone please clarify ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top