Read Concern 读取关注"linearizable"
The query returns data that reflects all successful majority-acknowledged writes that completed prior to the start of the read operation. 查询返回的数据反映了在读取操作开始之前完成的所有成功的多数确认写入。The query may wait for concurrently executing writes to propagate to a majority of replica set members before returning results.在返回结果之前,查询可以等待并发执行写入以传播到大多数副本集成员。
If a majority of your replica set members crash and restart after the read operation, documents returned by the read operation are durable if 如果大多数副本集成员在读取操作后崩溃并重新启动,则如果writeConcernMajorityJournalDefault
is set to the default state of true
.writeConcernMajorityJournalDefault
设置为默认状态true
,则读取操作返回的文档是持久的。
With 在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"
写入写入磁盘日志。As such, 因此,在给定副本集中的大多数节点发生瞬时丢失(例如崩溃和重新启动)的情况下,"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"
写入操作可能会回滚。
You can specify linearizable read concern for read operations on the 您可以仅在primary
only.primary
上为读取操作指定可线性化的读取关注。
Linearizable read concern guarantees only apply if read operations specify a query filter that uniquely identifies a single document.只有当读取操作指定了唯一标识单个文档的查询筛选器时,才适用线性化读取关注保证。
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.确保操作不会无限期阻塞,而是确保在无法满足读取关注时操作返回错误。
Causally Consistent Sessions因果一致会话
Read concern 读取关注"linearizable"
is unavailable for use with causally consistent sessions."linearizable"
不可用于因果一致会话。
Aggregation Restriction聚合限制
You cannot use the 不能将$out
or the $merge
stage in conjunction with read concern "linearizable"
. $out
或$merge
阶段与读取关注"linearizable"
一起使用。That is, if you specify 也就是说,如果为"linearizable"
read concern for db.collection.aggregate()
, you cannot include either stages in the pipeline.db.collection.aggregate()
指定了"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"
读取关注使多个线程能够对单个文档执行读写操作,就好像单个线程实时执行这些操作一样;也就是说,用于这些读取和写入的对应时间表被认为是线性的。
Read Your Own Writes阅读自己的文章
Changed in version 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 { w: "majority" }
write concern, and then issue your read operation with primary
read preference, and either "majority"
or "linearizable"
read concern.
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. [1] As such, reads with linearizable read concern may be significantly slower than reads with "majority"
or "local"
read concerns.
Always use maxTimeMS
with linearizable read concern in case a majority of data bearing members are unavailable. 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.
For example:
db.restaurants.find( { _id: 5 } ).readConcern("linearizable").maxTimeMS(10000)
db.runCommand( {
find: "restaurants",
filter: { _id: 5 },
readConcern: { level: "linearizable" },
maxTimeMS: 10000
} )
[1] | In some circumstances, two nodes in a replica set may transiently believe that they are the primary, but at most, one of them will be able to complete writes with { w: "majority" } write concern. The node that can complete { 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. primary , and new writes to the former primary will eventually roll back.primary ,并且对前一个主要服务器的新写入最终会回滚。 |