Read Concern读取关注
On this page本页内容
The readConcern
option allows you to control the consistency and isolation properties of the data read from replica sets and replica set shards.readConcern
选项允许您控制从副本集和副本集分片读取的数据的一致性和隔离属性。
Through the effective use of write concerns and read concerns, you can adjust the level of consistency and availability guarantees as appropriate, such as waiting for stronger consistency guarantees, or loosening consistency requirements to provide higher availability.通过有效利用写入关注和读取关注,您可以适当调整一致性和可用性保证的级别,例如等待更强的一致性保证,或放松一致性要求以提供更高的可用性。
MongoDB drivers updated for MongoDB 3.2 or later support specifying read concern.为MongoDB 3.2或更高版本更新的MongoDB驱动程序支持指定读取关注。
Starting in MongoDB 4.4, replica sets and sharded clusters support setting a global default read concern. 从MongoDB 4.4开始,副本集和分片集群支持设置全局默认读取关注。Operations which do not specify an explicit read concern inherit the global default read concern settings. 未指定显式读取关注的操作将继承全局默认读取关注设置。See 有关更多信息,请参阅setDefaultRWConcern
for more information.setDefaultRWConcern
。
Read Concern Levels读取关注级别
The following read concern levels are available:以下读取关注级别可用:
level | |
---|---|
"local" | "local" is available for use with or without causally consistent sessions and transactions."local" 可用于或不用于因果一致的会话和事务。"local" reference page. "local" 参考页。 |
"available" | "available" is unavailable for use with causally consistent sessions and transactions."available" 不可用于因果一致的会话和事务。"available" read concern provides the lowest latency reads possible among the various read concerns. "available" 读取关注在各种读取关注中提供了尽可能低的延迟读取。"available" read concern can return orphaned documents when reading from a sharded collection. "available" 读取关注在从分片集合读取时可能会返回孤立文档。"local" ."local" 。"available" reference page. "available" 参考页。 |
"majority" | "majority" is comparable in performance cost to other read concerns."majority" 在性能成本上与其他读取关注相当。"majority" is available for use with or without causally consistent sessions and transactions."majority" 可在具有或不具有因果一致会话和事务的情况下使用。"majority" , replica sets must use WiredTiger storage engine."majority" 的读取关注级别,副本集必须使用WiredTiger存储引擎。
Note "majority" provides its guarantees only if the transaction commits with write concern "majority". "majority" 提交时,读关注"majority" 才提供其保证。"majority" read concern provides no guarantees about the data read in transactions. "majority" 读取关注无法保证事务中读取的数据。"majority" reference page. "majority" 参考页。 |
"linearizable" | writeConcernMajorityJournalDefault is set to the default state of true .writeConcernMajorityJournalDefault 设置为默认状态true ,则读取操作返回的文档是持久的。writeConcernMajorityJournalDefault set to false , MongoDB does not wait for w: "majority" writes to be written to the on-disk journal before acknowledging the writes. writeConcernMajorityJournalDefault 设置为false 的情况下,MongoDB不会等待w: "majority" 写入到磁盘日志中,然后再确认写入。"majority" write operations could possibly roll back in the event of a transient loss (e.g. crash and restart) of a majority of nodes in a given replica set. "majority" 写入操作可能会回滚。
$out or the $merge stage in conjunction with read concern "linearizable" . $out 或$merge 阶段与读取关注"linearizable" 一起使用。"linearizable" read concern for db.collection.aggregate() , you cannot include either stages in the pipeline.db.collection.aggregate() 指定了"linearizable" 读取关注,则不能在管道中包含这两个阶段。Tip maxTimeMS with linearizable read concern in case a majority of data bearing members are unavailable. maxTimeMS 与线性化读取关注一起使用。maxTimeMS ensures that the operation does not block indefinitely and instead ensures that the operation returns an error if the read concern cannot be fulfilled. maxTimeMS 确保操作不会无限期阻塞,而是确保在无法满足读取关注时操作返回错误。"linearizable" reference page. "linearizable" 参考页。 |
"snapshot" | "snapshot" returns majority-committed data as it appears across shards from a specific single point in time in the recent past. "snapshot" 的查询返回最近某个特定时间点的分片中出现的多数提交数据。"snapshot" provides its guarantees only if the transaction commits with write concern "majority" . "majority" 提交时,读取关注"snapshot" 才提供其保证。"majority" , the transaction operations are guaranteed to have read from a snapshot of majority-committed data."majority" ,则保证事务操作已从多数提交数据的快照中读取。"majority" , the transaction operations are guaranteed to have read from a snapshot of majority-committed data that provides causal consistency with the operation immediately preceding the transaction start."majority" ,则保证事务操作已从多数提交数据的快照中读取,该快照提供了与事务启动前操作的因果一致性。"snapshot" is available for "snapshot" 可用于
|
Regardless of the read concern level, the most recent data on a node may not reflect the most recent version of the data in the system.无论读取关注级别如何,节点上的最新数据都可能无法反映系统中数据的最新版本。
For more information on each read concern level, see:有关每个读取关注级别的更多信息,请参阅:
readConcern
Support支持
Read Concern Option读取关注选项
For operations not in multi-document transactions, you can specify a 对于不在多文档事务中的操作,可以指定readConcern
level as an option to commands and methods that support read concern:readConcern
级别作为支持读取关注的命令和方法的选项:
readConcern: { level: <level> }
To specify the read concern level for 要指定mongosh
method db.collection.find()
, use the cursor.readConcern()
method:mongosh
方法db.collection.find()
的读取关注级别,请使用cursor.readConcern()
方法:
db.collection.find().readConcern(<level>)
Transactions and Available Read Concerns事务和可用读取关注
For multi-document transactions, you set the read concern at the transaction level, not at the individual operation level. 对于多文档事务,您可以在事务级别设置读取关注,而不是在单个操作级别。The operations in the transaction will use the transaction-level read concern. 事务中的操作将使用事务级别的读取关注。Any read concern set at the collection and database level is ignored inside the transaction. 在集合和数据库级别设置的任何读取关注在事务内部都将被忽略。If the transaction-level read concern is explicitly specified, the client level read concern is also ignored inside the transaction.如果显式指定了事务级别的读取关注,那么在事务内部也会忽略客户端级别的读取关心点。
Do not explicitly set the read concern for the individual operations. 不要为单个操作显式设置读取关注。To set the read concern for transactions, see Read Concern/Write Concern/Read Preference.要设置事务的读取关注,请参阅读取关注/写入关注/读取首选项。
You can set the read concern at the transaction start:您可以在事务开始时设置读取关注:
For multi-document transaction, the following read concern levels are available:对于多文档事务,可以使用以下读取关注级别:Write commands that are part of a multi-document transactions can support the transaction-level read concern.作为多文档事务一部分的写入命令可以支持事务级别的读取关注。Starting in MongoDB 4.4, you can create collections and indexes inside a transaction.从MongoDB 4.4开始,您可以在事务中创建集合和索引。If explicitly creating a collection or an index, the transaction must use read concern如果显式创建集合或索引,则事务必须使用读取关注"local"
."local"
。Implicit creation of a collection can use any of the read concerns available for transactions.隐式创建集合可以使用任何可用于事务的读取关注。
If unspecified at the transaction start, transactions use the session-level read concern or, if that is unset, the client-level read concern.如果在事务开始时未指定,则事务将使用会话级读取关注,如果未设置,则使用客户端级读取关注。
For more information, see Transaction Read Concern.有关更多信息,请参阅事务读取关注。
Causally Consistent Sessions and Available Read Concerns因果一致的会话和可用的阅读取关注
For operations in a causally consistent session, 对于因果一致会话中的操作,可以使用"local"
, "majority"
, and "snapshot"
levels are available. However, to guarantee causal consistency, you must use "majority"
. "local"
、"majority"
和"snapshot"
级别。但是,为了保证因果关系的一致性,必须使用"majority"
。For details, see Causal Consistency.有关详细信息,请参阅因果一致性。
Operations That Support Read Concern支持读取关注的操作
The following operations support read concern:以下操作支持读取关注:
To set read concern for operations in a transaction, you set the read concern at the transaction level, not at the individual operation level. 要为事务中的操作设置读取关注,请在事务级别设置读取关注,而不是在单个操作级别。Do not explicitly set the read concern for the individual operations in a transaction. 不要为事务中的各个操作显式设置读取关注。For more information, see Transactions and Read Concern.有关更多信息,请参阅事务和读取关注。
"local" | "available" | "majority" | "snapshot" [3] | "linearizable" | |
---|---|---|---|---|---|
count | ✓ | ✓ | ✓ | ✓ | |
distinct | ✓ | ✓ | ✓ | ✓ [2] | ✓ |
find | ✓ | ✓ | ✓ | ✓ | ✓ |
db.collection.find() via cursor.readConcern() | ✓ | ✓ | ✓ | ✓ | ✓ |
getMore | ✓ | ✓ | |||
aggregate db.collection.aggregate() | ✓ | ✓ | ✓ | ✓ | ✓ [6] |
Session.startTransaction() | ✓ | ✓ | ✓ |
[1] | $out or the $merge stage in conjunction with read concern "linearizable" . $out 或$merge 阶段与读取关注"linearizable" 一起使用。"linearizable" read concern for db.collection.aggregate() , you cannot include either stages in the pipeline.db.collection.aggregate() 指定了"linearizable" 读取关注,则不能在管道中包含这两个阶段。 |
[2] | "snapshot" is available only for certain read operations and for multi-document transactions. "snapshot" 仅适用于某些读取操作和多文档事务。distinct command or its helpers on a sharded collection.distinct 命令或其助手。 |
The following write operations can also accept a read concern if part of a multi-document transaction:如果是多文档事务的一部分,则以下写入操作也可以接受读取关注:
To set read concern for operations in a transaction, you set the read concern at the transaction level, not at the individual operation level.要为事务中的操作设置读取关注,请在事务级别设置读取关注,而不是在单个操作级别。
[3] | (1, 2) "snapshot" is available only for certain read operations and multi-document transactions. "snapshot" 仅适用于某些读取操作和多文档事务。"snapshot" correspond to the CRUD operations available in transactions. "snapshot" 的事务操作对应于事务中可用的CRUD操作。 |
Read Concern not Supported on local
Databaselocal
数据库不支持读取关注
local
DatabaseThe local database does not support read concerns. local
数据库不支持读取关注。MongoDB silently ignores any configured read concern for an operation on a collection in the local database.MongoDB静默地忽略本地数据库中集合上操作的任何配置的读取关注。
Considerations注意事项
Read Your Own Writes读取自己的写入
Changed in version 3.63.6版更改.
Starting in MongoDB 3.6, you can use causally consistent sessions to read your own writes, if the writes request acknowledgement.从MongoDB 3.6开始,如果写入请求确认,您可以使用因果一致会话来读取自己的写入。
Prior to MongoDB 3.6, in order to read your own writes you must issue your write operation with 在MongoDB 3.6之前,为了读取您自己的写入,您必须发出具有{ w: "majority" }
write concern, and then issue your read operation with primary
read preference, and either "majority"
or "linearizable"
read concern.{ w: "majority" }
写入关注的写入操作,然后发出具有primary
读取偏好的读取操作,以及"majority"
或"linearizable"
读取关注。
Real Time Order实时排序
Combined with 与"majority"
write concern, "linearizable"
read 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."majority"
写入关注相结合,"linearizable"
读取关注使多个线程能够对单个文档执行读写操作,就好像单个线程实时执行这些操作一样;也就是说,这些读取和写入的相应时间表被认为是线性的。
Performance Comparisons性能比较
Unlike 与"majority"
, "linearizable"
read concern confirms with secondary members that the read operation is reading from a primary that is capable of confirming writes with { w: "majority" }
write concern. "majority"
不同,"linearizable"
读取关注向次要成员确认读取操作是从能够通过{ w: "majority" }
写入关注确认写入的主要成员读取。[4] As such, reads with linearizable read concern may be significantly slower than reads with 因此,具有线性化阅读取关注的阅读可能比具有"majority"
or "local"
read concerns."majority"
或"local"
阅读取关注的读取慢得多。
Always use 在大多数数据承载成员不可用的情况下,始终将maxTimeMS
with linearizable read concern in case a majority of data bearing members are unavailable. maxTimeMS
与线性化读取关注一起使用。maxTimeMS
ensures that the operation does not block indefinitely and instead ensures that the operation returns an error if the read concern cannot be fulfilled.maxTimeMS
确保操作不会无限期阻塞,而是确保在无法满足读取关注时操作返回错误。
For example:例如:
db.restaurants.find( { _id: 5 } ).readConcern("linearizable").maxTimeMS(10000)
db.runCommand( {
find: "restaurants",
filter: { _id: 5 },
readConcern: { level: "linearizable" },
maxTimeMS: 10000
} )
[4] | { w: "majority" } write concern. { w: "majority" } 写入关注的写入。{ w: "majority" } writes is the current primary, and the other node is a former primary that has not yet recognized its demotion, typically due to a network partition. { w: "majority" } 写入的节点是当前主节点,而另一个节点是尚未识别其降级的前主节点,通常是由于网络分区。primary , and new writes to the former primary will eventually roll back.primary 的新写入最终会回滚。 |
Read Operations and 读取操作和afterClusterTime
MongoDB 3.6 introduces support for causally consistent sessions. MongoDB 3.6引入了对因果一致会话的支持。For read operations associated with causally consistent session, MongoDB 3.6 introduces the 对于与原因一致会话相关的读取操作,MongoDB 3.6引入了afterClusterTime
read concern option to be set automatically by the drivers for operations associated with causally consistent sessions.afterClusterTime
读取关注选项,由驱动程序为与原因一致的会话相关的操作自动设置。
Do not manually set 不要为读取操作手动设置afterClusterTime
for a read operation. MongoDB drivers set this value automatically for operations associated with causally consistent sessions. afterClusterTime
。MongoDB驱动程序为与因果一致会话相关联的操作自动设置此值。However, you can advance the operation time and the cluster time for the session, such as to be consistent with the operations of another client session. 但是,您可以提前会话的操作时间和集群时间,以便与另一个客户端会话的操作保持一致。For an example, see Examples.有关示例,请参阅示例。
It is not possible to specify atClusterTime in conjunction with 不能将afterClusterTime
. atClusterTime
与afterClusterTime
一起指定。To use atClusterTime with read concern 要将"snapshot"
you have to disable causally consistent sessions.atClusterTime
与读取关注"snapshot"
一起使用,必须禁用因为一致性会话。
To satisfy a read request with an 要满足afterClusterTime
value of T
, a mongod
must perform the request after its oplog reaches time T
. afterClusterTime
值为T
的读取请求,mongod
必须在其oplog达到时间T
后执行该请求。If its oplog has not reached time 如果它的oplog还没有到达时间T
, the mongod
must wait to service the request.T
,mongod
必须等待为请求提供服务。
Read operations with a specified 具有指定afterClusterTime
return data that meet both the read concern level requirement and the specified afterClusterTime
requirement.afterClusterTime
的读取操作返回的数据同时满足读取关注级别要求和指定的afterClusterTime
要求。
For read operations not associated with causally consistent sessions, 对于不与因果一致会话关联的读取操作,afterClusterTime
is unset.afterClusterTime
是未设置的。
Read Concern Provenance读取关注来源
Starting in version 4.4, MongoDB tracks read concern 从4.4版本开始,MongoDB跟踪读取关注的来源,这表明了特定读取关注的provenance
, which indicates the source of a particular read concern. provenance
。You may see 您可能会在provenance
shown in the getLastError
metrics, read concern error objects, and MongoDB logs.getLastError
度量、读取关注错误对象和MongoDB日志中看到provenance
。
The following table shows the possible read concern 下表显示了可能读取的关注provenance
values and their significance:provenance
值及其重要性:
clientSupplied | |
customDefault | setDefaultRWConcern .setDefaultRWConcern 。 |
implicitDefault |