该查询返回的数据反映了在读取操作开始之前完成的所有成功的多数已确认的写入操作。该查询可能会等待并发执行的写入操作的数据复制到多数副本集成员之后,再返回结果。
如果多数副本集成员在读操作后崩溃并重新启动,且 writeConcernMajorityJournalDefault 设置为默认状态 true,则读操作返回的文档是持久化的。
将 writeConcernMajorityJournalDefault 设置为 false 时,MongoDB 不会等待 w: "majority" 写入在写入到磁盘日志后才确认写入。例如,"majority" 写操作可能会在给定副本集中的大部分节点发生临时断连(例如崩溃和重启)的情况下回滚。
您只能为primary上的读取操作指定线性化读关注。
提示
始终将maxTimeMS与线性化读关注一起使用,以防大多数数据承载成员不可用。maxTimeMS确保操作不会无限期受阻,而是确保操作在无法满足读关注时返回错误。
要求
仅在读操作指定唯一标识单个文档的查询筛选条件时,才适用线性化读关注保证。此外,如果不满足以下条件,则线性化读关注可能无法从一致的快照中读取,从而导致未返回与过滤器匹配的文档:
如果满足前述任何条件,查询将从一致快照中读取,返回单个匹配文档。
因果一致的会话
Read concern "linearizable" is unavailable for use with causally consistent sessions.
聚合限制
You cannot use the $out or the $merge stage in conjunction with read concern "linearizable". That is, if you specify "linearizable" read concern for db.collection.aggregate(), you cannot include either stages in the pipeline.
实时顺序
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 concern)会与从从节点(secondary node from replica set)成员确认读取操作正在从能够确认具有 写关注(write concern)的写入操作的主节点 (primary{ w: "majority" } node in the replica set)中读取。1 [] 因此,具有可线性化读关注(read concern)的读取可能比具有"majority" 或"local" 读关注的读取慢得多。
始终将maxTimeMS与线性化读关注一起使用,以防大多数数据承载成员不可用。maxTimeMS确保操作不会无限期受阻,而是确保操作在无法满足读关注时返回错误。
例如:
db.restaurants.find( { _id: 5 } ).readConcern("linearizable").maxTimeMS(10000) db.runCommand( { find: "restaurants", filter: { _id: 5 }, readConcern: { level: "linearizable" }, maxTimeMS: 10000 } )
| [1] | 在某些情况下,副本集中的两个节点可能会暂时认为它们是主节点,但最多只能有其中一个节点能够完成具有 |