mongoDB transaction

mongoDB transaction

Atomicity

In MongoDB, a write operation is atomic on the level of a single document, even if the operation modifies multiple embedded documents within a single document.

When a single write operation (e.g. db.collection.updateMany()) modifies multiple documents, the modification of each document is atomic, but the operation as a whole is not atomic.

https://docs.mongodb.com/manual/core/write-operations-atomicity/


Concurrency control

Concurrency control allows multiple applications to run concurrently without causing data inconsistency or conflicts.

One approach is to specify the expected current value of a field in the query predicate for the write operations.

类似于乐观锁控制


Transaction Support

  • In version 4.0, MongoDB supports multi-document transactions on replica sets.
  • In version 4.2, MongoDB introduces distributed transactions, which adds support for multi-document transactions on sharded clusters and incorporates the existing support for multi-document transactions on replica sets.

https://docs.mongodb.com/manual/core/transactions/