Question

Is there a way to perform a set of non-atomic actions on MongoDB server (using the Ruby driver) as an single atomic action? What I need is basically a lock on certain object/collection.

Was it helpful?

Solution

There's no way to do it in the Ruby driver because there's no way to do it in MongoDB. Mongo only supports single-document atomic operations. So basically an insert, update or delete of a single document is done atomically, but not operations across multiple documents.

You might be able to fake a transaction by attempting a manual "roll-back" if an error occurs. A roll-back in this case would be to replace any changes with the previous values. But that's going to manual and not have the ACID guarantees that you would get from most SQL servers.

OTHER TIPS

Because you can perform atomic operations on single documents, there are ways to simulate what you want. See this article:

http://kylebanker.com/blog/2010/06/07/mongodb-inventory-transactions/

And for some of the principles behind the ideas there, see this one:

http://www.eaipatterns.com/docs/IEEE_Software_Design_2PC.pdf

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