Atomicity and Transactions原子性与事务
On this page本页内容
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.在MongoDB中,写操作是单个文档级别的原子操作,即使该操作修改了单个文档中的多个嵌入文档。
Multi-Document Transactions多文档事务处理
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.db.collection.updateMany()
)修改多个文档时,每个文档的修改都是原子性的,但整个操作不是原子性的。
When performing multi-document write operations, whether through a single write operation or multiple write operations, other operations may interleave.当执行多文档写入操作时,无论是通过单个写入操作还是通过多个写入操作,其他操作都可能交错。
For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions:对于需要对多个文档(在单个或多个集合中)进行原子性读写的情况,MongoDB支持多文档事务:
- In version 4.0
, MongoDB supports multi-document transactions on replica sets.,MongoDB支持副本集上的多文档事务。 - 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.,MongoDB引入了分布式事务,增加了对分片集群上多文档事务的支持,并整合了对副本集上多文档事务的现有支持。
For details regarding transactions in MongoDB, see the transactions page.有关MongoDB中事务的详细信息,请参阅事务页面。
In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design. 在大多数情况下,与单文档写入相比,多文档事务会产生更高的性能成本,并且多文档事务的可用性不应取代有效的模式设计。For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. 对于许多场景,非规范化数据模型(嵌入文档和数组)将继续是您的数据和用例的最佳选择。That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.也就是说,对于许多场景,对数据进行适当建模将最大限度地减少对多文档事务的需求。
For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.有关其他事务使用注意事项(如运行时限制和操作日志大小限制),请参阅生产注意事项。
Concurrency Control并发控制
Concurrency control allows multiple applications to run concurrently without causing data inconsistency or conflicts.并发控制允许多个应用程序并行运行,而不会导致数据不一致或冲突。
A 对文档的findAndModify
operation on a document is atomic: if the find condition matches a document, the update is performed on that document. findAndModify
操作是原子操作:如果查找条件与文档匹配,则对该文档执行更新。Concurrent queries and additional updates on that document are not affected until the current update is complete.在当前更新完成之前,对该文档的并发查询和其他更新不会受到影响。
Consider the following example:考虑以下示例:
A collection with two documents:包含两个文档的集合:db.myCollection.insertMany( [
{ _id: 0, a: 1, b: 1 },
{ _id: 1, a: 1, b: 1 }
] )Two of the following以下两个findAndModify
operations run concurrently:findAndModify
操作同时运行:db.myCollection.findAndModify( {
query: { a: 1 },
update: { $inc: { b: 1 }, $set: { a: 2 } }
} )
After the findAndModify
operations are complete, it is guaranteed that a
and b
in both documents are set to 2
.findAndModify
操作完成后,可以保证两个文档中的a
和b
都设置为2
。
See also: 另请参阅:
You can also create a unique index on a field so that it can only have unique values. 也可以在字段上创建唯一索引,使其只能具有唯一值。This prevents inserts and updates from creating duplicate data. 这样可以防止插入和更新创建重复数据。You can create a unique index on multiple fields to ensure the combination of field values is unique. 可以在多个字段上创建唯一索引,以确保字段值的组合是唯一的。For an example, see findAndModify() Upsert with Unique Index.有关示例,请参阅findAndModify()
Upsert
使用唯一索引。