On this page本页内容
Depending on the read concern, clients can see the results of writes before the writes are durable:根据读关注点的不同,客户端可以在写入持久之前查看写入的结果:
"local"
or "available"
read concern can see the result of a write operation before the write operation is acknowledged to the issuing client."local"
或"available"
读关注点的其他客户端都可以在向发出请求的客户端确认写操作之前看到写操作的结果。"local"
or "available"
read concern can read data which may be subsequently rolled back during replica set failovers."local"
或"available"
读取关注点的客户端可以读取数据,这些数据可能会在副本集故障切换期间随后回滚。For operations in a multi-document transaction, when a transaction commits, all data changes made in the transaction are saved and visible outside the transaction. That is, a transaction will not commit some of its changes while rolling back others.
Until a transaction commits, the data changes made in the transaction are not visible outside the transaction.在事务提交之前,事务中所做的数据更改在事务外部不可见。
However, when a transaction writes to multiple shards, not all outside read operations need to wait for the result of the committed transaction to be visible across the shards. 但是,当事务写入多个分片时,并非所有外部读取操作都需要等待提交的事务的结果在分片中可见。For example, if a transaction is committed and write 1 is visible on shard A but write 2 is not yet visible on shard B, an outside read at read concern 例如,如果事务已提交,且写入1在碎片a上可见,但写入2在碎片B上尚不可见,则外部读取-读取关注点"local"
can read the results of write 1 without seeing write 2."local"
可以读取写入1的结果,而不查看写入2。
Read uncommitted is the default isolation level and applies to 读取未提交是默认隔离级别,适用于mongod
standalone instances as well as to replica sets and sharded clusters.mongod
独立实例以及副本集和分片集群。
Write operations are atomic with respect to a single document; i.e. if a write is updating multiple fields in the document, a read operation will never see the document with only some of the fields updated. 写操作是针对单个文档的原子操作;亦即,如果写入操作正在更新文档中的多个字段,则读取操作将永远不会看到仅更新了部分字段的文档。However, although a client may not see a partially updated document, read uncommitted means that concurrent read operations may still see the updated document before the changes are made durable.但是,尽管客户端可能看不到部分更新的文档,但read uncommitted意味着并发读取操作仍可能在更改变得持久之前看到更新的文档。
With a standalone 对于独立的mongod
instance, a set of read and write operations to a single document is serializable. mongod
实例,对单个文档的一组读写操作是可序列化的。With a replica set, a set of read and write operations to a single document is serializable only in the absence of a rollback.对于副本集,对单个文档的一组读写操作只能在没有回滚的情况下序列化。
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中事务的详细信息,请参阅事务页面。
Important重要的
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大小限制),请参阅生产注意事项。
Without isolating the multi-document write operations, MongoDB exhibits the following behavior:在不隔离多文档写入操作的情况下,MongoDB表现出以下行为:
MongoDB cursors can return the same document more than once in some situations. 在某些情况下,MongoDB游标可以多次返回同一文档。As a cursor returns documents, other operations may interleave with the query. 当游标返回文档时,其他操作可能会与查询交错。If one of these operations changes the indexed field on the index used by the query, then the cursor could return the same document more than once.如果其中一个操作更改了查询使用的索引上的索引字段,则游标可以多次返回同一文档。
If your collection has a field or fields that are never modified, you can use a unique index on this field or these fields so that the query will return each document no more than once. 如果集合中有一个或多个从未修改过的字段,则可以在此字段或这些字段上使用唯一索引,以便查询只返回每个文档一次。Query with 使用hint()
to explicitly force the query to use that index.hint()
查询显式强制查询使用该索引。
MongoDB provides monotonic write guarantees, by default, for standalone 默认情况下,MongoDB为独立mongod
instances and replica set.mongod
实例和副本集提供单调写入保证。
For monotonic writes and sharded clusters, see Causal Consistency.有关分片一致性,请参阅因果一致性。
New in version 3.4.版本3.4中的新功能。
For read and write operations on the primary, issuing read operations with 对于主线程上的读写操作,发出具有"linearizable"
read concern and write operations with "majority"
write concern enables multiple threads to perform reads and writes on a single document as if a single thread performed these operations in real time; that is, the corresponding schedule for these reads and writes is considered linearizable."linearizable"
读关注点的读操作和具有"majority"
写关注点的写操作,使多个线程能够对单个文档执行读写操作,就像单个线程实时执行这些操作一样;也就是说,这些读写的相应调度被认为是可线性化的。
See also参阅
New in version 3.6.版本3.6中的新功能。
If an operation logically depends on a preceding operation, there is a causal relationship between the operations. 如果一个操作在逻辑上依赖于前面的操作,那么两个操作之间存在因果关系。For example, a write operation that deletes all documents based on a specified condition and a subsequent read operation that verifies the delete operation have a causal relationship.例如,基于指定条件删除所有文档的写入操作和验证删除操作是否存在因果关系的后续读取操作。
With causally consistent sessions, MongoDB executes causal operations in an order that respect their causal relationships, and clients observe results that are consistent with the causal relationships.通过因果一致性会话,MongoDB按照尊重因果关系的顺序执行因果操作,客户观察到的结果与因果关系一致。
To provide causal consistency, MongoDB 3.6 enables causal consistency in client sessions. 为了提供因果一致性,MongoDB 3.6在客户端会话中启用因果一致性。A causally consistent session denotes that the associated sequence of read operations with "majority"
read concern and write operations with "majority"
write concern have a causal relationship that is reflected by their ordering. Applications must ensure that only one thread at a time executes these operations in a client session.
For causally related operations:对于因果相关的操作:
Important重要的
Client sessions only guarantee causal consistency for:客户端会话仅保证以下情况的因果一致性:
"majority"
; i.e. the return data has been acknowledged by a majority of the replica set members and is durable."majority"
读取操作;亦即,返回数据已被大多数副本集成员确认,并且是持久的。"majority"
write concern; i.e. the write operations that request acknowledgement that the operation has been applied to a majority of the replica set’s voting members."majority"
写入关注点的写入操作;亦即,请求确认操作已应用于副本集的大多数投票成员的写入操作。For more information on causal consistency and various read and write concerns, see Causal Consistency and Read and Write Concerns.有关因果一致性和各种读写关注点的更多信息,请参阅因果一致性和读写关注点。
"majority"
read concern and write operations (with "majority"
write concern), the client includes the session information with each operation."majority"
read concern and write operation with "majority"
write concern associated with the session, MongoDB returns the operation time and the cluster time, even if the operation errors. Note
MongoDB does not return the operation time and the cluster time for unacknowledged (MongoDB不会返回未确认(w: 0
) write operations. w: 0
)写入操作的操作时间和群集时间。Unacknowledged writes do not imply any causal relationship.未确认的书写并不意味着任何因果关系。
Although, MongoDB returns the operation time and the cluster time for read operations and acknowledged write operations in a client session, only the read operations with "majority"
read concern and write operations with "majority"
write concern can guarantee causal consistency. For details, see Causal Consistency and Read and Write Concerns.
Note
Operations can be causally consistent across different sessions. 不同会话之间的操作可以是因果一致的。MongoDB drivers and the MongoDB驱动程序和mongo
shell provide the methods to advance the operation time and the cluster time for a client session. mongo
Shell提供了提前客户端会话的操作时间和集群时间的方法。So, a client can advance the cluster time and the operation time of one client session to be consistent with the operations of another client session.因此,客户机可以提前一个客户机会话的集群时间和操作时间,以与另一个客户机会话的操作一致。
The following table lists the causal consistency guarantees provided by causally consistent sessions for read operations with 下表列出了因果一致性会话为具有"majority"
read concern and write operations with "majority"
write concern."majority"
读关注点的读操作和具有"majority"
写关注点的写操作提供的因果一致性保证。
Guarantees | |
---|---|
then read2 cannot return results of write1. | |
For example, if write1 must precede write2 in a session, the state of the data at the time of write2 must reflect the state of the data post write1. Other writes can interleave between write1 and write write2, but write2 cannot occur before write1. | |
These guarantees hold across all members of the MongoDB deployment. For example, if, in a causally consistent session, you issue a write with "majority"
write concern followed by a read that reads from a secondary (i.e. read preference secondary
) with "majority"
read concern, the read operation will reflect the state of the database after the write operation.
Operations within a causally consistent session are not isolated from operations outside the session. 因果一致会话内的操作不会与会话外的操作隔离。If a concurrent write operation interleaves between the session’s write and read operations, the session’s read operation may return results that reflect a write operation that occurred after the session’s write operation.如果并发写操作在会话的写操作和读操作之间交错,则会话的读操作可能返回反映在会话写操作之后发生的写操作的结果。
Tip
Applications must ensure that only one thread at a time executes these operations in a client session.应用程序必须确保在客户端会话中一次只有一个线程执行这些操作。
Clients require MongoDB drivers updated for MongoDB 3.6 or later:客户端需要为MongoDB 3.6或更高版本更新MongoDB驱动程序:
Java 3.6+ Python 3.6+ C 1.9+ |
C# 2.5+ Node 3.0+ Ruby 2.5+ |
Perl 2.0+ PHPC 1.4+ Scala 2.2+ |
Important
Causally consistent sessions can only guarantee causal consistency for reads with 因果一致性会话只能保证具有"majority"
read concern and writes with "majority"
write concern."majority"
读关注点的读取和具有"majority"
写关注点的写入的因果一致性。
Consider a collection 考虑一个集合items
that maintains the current and historical data for various items. items
,该项保存各种项的当前和历史数据。Only the historical data has a non-null 只有历史数据具有非空的end
date. end
日期。If the 如果某个物料的sku
value for an item changes, the document with the old sku
value needs to be updated with the end
date, after which the new document is inserted with the current sku
value. sku
值发生更改,则需要使用结束日期更新具有旧sku
值的文档,然后使用当前sku
值插入新文档。The client can use a causally consistent session to ensure that the update occurs before the insert.客户端可以使用因果一致的会话来确保在插入之前进行更新。
If another client needs to read all current 如果另一个客户端需要读取所有当前sku
values, you can advance the cluster time and the operation time to that of the other session to ensure that this client is causally consistent with the other session and read after the two writes:sku
值,您可以将群集时间和操作时间提前到另一个会话的时间,以确保此客户端与另一个会话因果一致,并在两次写入后读取:
The following operations that build in-memory structures are not causally consistent:以下内置内存结构的操作在原因上不一致:
collStats |
|
$collStats with latencyStats option. |
|
$currentOp |
|
createIndexes |
|
dbHash |
|
dbStats |
|
getMore |
|
$indexStats |
|
mapReduce |
|
ping |
|
serverStatus |
|
validate |