Is mongoDB's findAndModify “transaction-save”

匆匆过客 提交于 2019-12-23 02:51:21

问题


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.


回答1:


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.




回答2:


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 ?



来源:https://stackoverflow.com/questions/11331227/is-mongodbs-findandmodify-transaction-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!