ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Docs 菜单

读关注 "local"

具有 "local" 读关注的查询从实例中返回数据,但不保证数据已写入到大多数副本集节点(即可能会回滚)。

"local" 读关注是针对主节点和从节点的读取操作的默认设置。

无论读关注级别如何,节点上的最新数据可能无法反映系统中数据的最新版本。

无论有没有因果一致的会话和事务,均可使用读关注 "local"

您可以在事务级别设置读关注,而不是在单个操作级别。要为事务设置读关注,请参阅事务和读关注(read concern)。

您可以在事务中创建集合和索引。如果显式创建集合或索引,则事务必须使用读关注 "local"。如果 隐式 创建集合,则可以使用任何可用于事务的读关注。

在副本集上,即使ACID 事务使用读关注(read concern)local ,您也可能会观察到更强的读隔离性性,因为该操作会从ACID 事务打开点的快照中读取。

考虑以下对三节点副本集执行 Write 0 写入操作的时间轴:

注意

为简单起见,该示例预设以下条件:

  • Write 0 之前的所有写入均已成功复制到所有成员。

  • Write prev 是 Write 0 前一次的写入操作。

  • Write 0 之后没有发生其他写入操作。

Timeline of a write operation to a three member replica set
时间
事件
最新写入
最新 w: "majority" 写入

t 0

主节点应用 Write 0

Primary: Write 0
Secondary 1: Write prev
Secondary 2: Write prev
Primary: Write prev
Secondary 1: Write prev
Secondary 2: Write prev

t 1

从节点 1 应用 write 0

Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write prev
Primary: Write prev
Secondary 1: Write prev
Secondary 2: Write prev

t 2

从节点 2 应用 write 0

Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
Primary: Write prev
Secondary 1: Write prev
Secondary 2: Write prev

t 3

主节点知道已成功复制到从节点 1,并向客户端发送确认

Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
Primary: Write 0
Secondary 1: Write prev
Secondary 2: Write prev

t 4

主节点感知到数据已成功复制到从节点 2

Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
Primary: Write 0
Secondary 1: Write prev
Secondary 2: Write prev

t 5

从节点 1 收到通知(通过常规复制机制),要求更新其最新 w: "majority" 写入的快照

Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write prev

t 6

从节点 2 收到通知(通过常规复制机制),要求更新其最新 w: "majority" 写入的快照

Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0

然后,下表汇总了具有 "local" 读关注的读取操作在时间 T 时看到的数据状态。

Timeline of a write operation to a three member replica set.
读取目标
时间 T
数据状态

主节点

t 之后 0

数据反映写入 0

从节点 1

t 之前 1

数据反映为 Write prev 应用后的状态

从节点 1

t 之后 1

数据反映为 Write 应用后的状态 0

从节点 2

t 之前 2

数据反映为 Write prev 应用后的状态

从节点 2

t 之后 2

数据反映为 Write 应用后的状态 0