Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB transactions?

Playing around with MongoDB and NoRM in .NET.

Thing that confused me - there are no transactions
(can't just tell MongoConnection.Begin/EndTransaction or something like that).

I want to use Unit of work pattern and rollback changes in case something fails.

Is there still a clean way how to enrich my repository with ITransaction?

like image 645
Arnis Lapsa Avatar asked Apr 16 '10 18:04

Arnis Lapsa


People also ask

Can MongoDB do transactions?

For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.

Is MongoDB good for transactional database?

MongoDB doesn't support transactions, but saving one document is atomic. So, it is better to design you database schema in such a way, that all the data needed to be saved atomically will be placed in one document.

Does MongoDB support ACID transactions?

However, not all databases support transactions that work across multiple records, which can be concerning to developers who are accustomed to using them. But, MongoDB supports multi-document MongoDB ACID transactions for the use cases that require them.


4 Answers

MongoDB doesn't support complex multi-document transactions. If that is something you absolutely need it probably isn't a great fit for you.

In most cases, however, we've found that complex transactions aren't a requirement. All operations in MongoDB are atomic on a single document, and we support nice update modifiers, which make a lot of operations that would need a transaction easy to implement (and fast).

like image 89
mdirolf Avatar answered Sep 24 '22 04:09

mdirolf


That is true that MongoDB does't support transaction out of the box, but you can implement optimistic transactions on your own. They fit fine the unit of work. I wrote an java example and some explanation on a GitHub so you can easily repeat in C#.

like image 30
rystsov Avatar answered Sep 24 '22 04:09

rystsov


As of v4.0, MongoDB supports multi-document ACID transactions. Through snapshot isolation, transactions provide a globally consistent view of data, and enforce all-or-nothing execution to maintain data integrity. For more info, see https://www.mongodb.com/transactions

In 4.2, MongoDB will also support sharded transactions.

In this blog post, I also outline our journey to multi-document ACID transactions, in case if you are interested in the history and our reasoning.

like image 24
Grigori Melnik Avatar answered Sep 25 '22 04:09

Grigori Melnik


MongoDB 4.0 will add support for multi-document transactions.

https://www.mongodb.com/transactions

like image 20
Benjamin Lorenz Avatar answered Sep 27 '22 04:09

Benjamin Lorenz