On this page本页内容
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中,写入操作在单个文档级别上是原子的,即使该操作修改单个文档中的多个嵌入文档。
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支持多文档事务:
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.有关其他事务使用注意事项(如运行时限制和oplog大小限制),请参阅生产注意事项。
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
。
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。