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
。
The following read concern levels are available:以下读取关注级别可用:
"local" |
|
"available" |
|
"majority" |
|
"linearizable" |
|
"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
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>)
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:对于多文档事务,可以使用以下读取关注级别:
"local"
. "local"
读关注点。ImplicitIf 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.有关更多信息,请参阅事务读取关注。
For operations in a causally consistent session, 对于因果一致会话中的操作,可以使用"local"
, "majority"
, and "snapshot"
levels are available. "local"
、"majority"
和"snapshot"
级别。However, to guarantee causal consistency, you must use 然而,为了保证因果一致性,必须使用"majority"
. "majority"
。For details, see Causal Consistency.有关详细信息,请参阅因果一致性。
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.要为事务中的操作设置读取关注点,可以在事务级别而不是单个操作级别设置读取关注点。
"local" | "available" | "majority" | "snapshot" [3] | "linearizable" | |
---|---|---|---|---|---|
✓ | ✓ | ||||
✓ | ✓ | ||||
✓ | ✓ | ||||
✓ | ✓ | ||||
✓ | |||||
(Requires fCV 4.4 or greater)
| ✓ |
[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操作。 |
local
Databaselocal
数据库不支持读取关注The 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会自动忽略本地数据库中集合操作的任何已配置读取问题。
Changed in version 3.6.在版本3.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"
读取关注点来执行读取操作。
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"
读关注点使多个线程能够对单个文档执行读写操作,就像单个线程实时执行这些操作一样;也就是说,这些读写的相应时间表被认为是可线性化的。
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 ,而对前primary 的新写入操作最终将回滚。 |
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. afterClusterTime
。MongoDB drivers set this value automatically for operations associated with causally consistent sessions. 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
未设置。
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 |