2
votes

I work with mongodb and I thing a possible strategy to save data in different collections, for example 2 collections. But, like a transactional system, I want ensure that data is present in collection1 and in collection2 or nothing.

In practicaly I want track operations that are performed into my application when execute something. So for example when my application save a data, I want also save that it has performed a insert operations, when modify data, I want track modify operation etc.

These operations data could save as embended document of single collection. For example if I try save data into collection1 then I can save "insert operation data" in same collection1 as embended document. If I try modify data into collection2 then I can save "modify operation data" in same collection2 as embended document, and so on... Thus when I want retrieve "operations embended document data" I must query collection1, collection2, collection3 etc etc. I think that is expensive. Further collection1, collection2, collection3 are already stressed by application, so I don't want stress furthermore.

Alternative, I can save "operation data" in separate collection, for example "oper_coll" collection name. So I can query only "oper_coll" for retrieve "operations data" but it implies that when I save data in collection1 I must save "operation data" in "oper_coll" for tracking current operation. I want data is saved correctly in collection1 and operation in oper_coll. If something goes wrong it do nothing, data doesn't present in either collection ( collection1 and oper_coll ). So I need a something as transactional system at application level because I know that mongodb doesn't support transactions.

So I want implement something similar at application level, because I need save data1 into collection1 and if it is ok, it save data2 into collection2. If something goes wrong rollback all.

Do you have some idea?

1

1 Answers

0
votes

First I think this link would help, it provides an example of how to orchestrate a 2 phase commit using mongo.

http://cookbook.mongodb.org/patterns/perform-two-phase-commits/

Based on the example in the link and some additional reading around the subject I've created a mongo style transaction using the unit of work design pattern.

Does that help at all?