Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve rollback in transactions in mongo? [closed]

I am using mongoose over mongodb.

In a request and response from nodejs express server, generally requires a no of queries to mongodb through mongoose.

In many cases it may happen, if first a few queries executes successfully and rest all failed under a transaction.

Now i need to rollback my db state to the very initial state of db.

So how can i rollback efficiently in mongodb? Please provide some suitable references.

like image 888
codeofnode Avatar asked Jan 31 '14 18:01

codeofnode


People also ask

Can we rollback transaction in MongoDB?

Changed in version 4.0. Starting in version 4.0, MongoDB has no limit on the amount of data that can be rolled back. In previous versions, a mongod instance will not roll back more than 300 megabytes of data and requires manual intervention if more than 300 megabytes of data need to be rolled back.

How do I rollback in MongoDB?

Rollback Data Collection This is done by ensuring rollback files are created (only available with MongoDB version 4.0) by enabling the createRollbackDataFiles. By default this option is set to true hence rollback files will always be created. The rollback files are placed in the path /rollback/ .

How can you achieve transaction in MongoDB?

Transactions use the transaction-level write concern to commit the write operations. Write operations inside transactions must be issued without explicit write concern specification and use the default write concern. At commit time, the writes are then commited using the transaction-level write concern.

How does MongoDB handle transactions or locking?

MongoDB uses multi-granularity locking [1] that allows operations to lock at the global, database or collection level, and allows for individual storage engines to implement their own concurrency control below the collection level (e.g., at the document-level in WiredTiger).


2 Answers

I agree with Hector. If you need multiple document transactions then Mongo might not be a good fit for you. Can you embed your data structure in a single document? Then you can get the rollback you are looking for.

Another option is to look at the Mongo clone TokuMX which provides transactions across multiple documents.

like image 154
Dharshan Avatar answered Nov 08 '22 15:11

Dharshan


Going through mongo tutorials

Here is a link i found for making two phase commits ( may be it is not guaranteed ) :

http://docs.mongodb.org/manual/tutorial/perform-two-phase-commits/

like image 44
codeofnode Avatar answered Nov 08 '22 16:11

codeofnode