对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

读关注 (read concern) "linearizable"

"linearizable"

该查询返回的数据反映了在读取操作开始之前完成的所有成功的多数已确认的写入操作。该查询可能会等待并发执行的写入操作的数据复制到多数副本集成员之后,再返回结果。

如果多数副本集成员在读操作后崩溃并重新启动,且 writeConcernMajorityJournalDefault 设置为默认状态 true,则读操作返回的文档是持久化的。

writeConcernMajorityJournalDefault 设置为 false 时,MongoDB 不会等待 w: "majority" 写入在写入到磁盘日志后才确认写入。例如,"majority" 写操作可能会在给定副本集中的大部分节点发生临时断连(例如崩溃和重启)的情况下回滚。

您只能为primary上的读取操作指定线性化读关注。

提示

始终将maxTimeMS与线性化读关注一起使用,以防大多数数据承载成员不可用。maxTimeMS确保操作不会无限期受阻,而是确保操作在无法满足读关注时返回错误。

仅在读操作指定唯一标识单个文档的查询筛选条件时,才适用线性化读关注保证。此外,如果不满足以下条件,则线性化读关注可能无法从一致的快照中读取,从而导致未返回与过滤器匹配的文档:

  • 该查询使用不可变字段作为查询的搜索键。例如,在 _id 字段上搜索或使用 $natural

  • 任何并发更新都不会改变查询的搜索键。

  • 搜索键具有唯一索引,并且查询使用该索引。

如果满足前述任何条件,查询将从一致快照中读取,返回单个匹配文档。

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]

某些情况下,副本集中的两个节点可能会暂时认为它们是主节点,但最多只能有其中一个节点能够完成具有{ w: "majority" }写入关注的写入操作。可以完成{ w: "majority" }写入操作的节点是当前主节点,另一个节点是尚未识别其降级(通常是由于网络分区)的前主节点。发生这种情况时,尽管已请求读取偏好primary,但连接到前主节点的客户端可能会观察到过时数据,并且对前主节点的新写入操作最终将回滚。

给本页内容打分