On this page本页内容
Read preference describes how MongoDB clients route read operations to the members of a replica set.读取首选项描述MongoDB客户端如何将读取操作路由到副本集的成员。
By default, an application directs its read operations to the primary member in a replica set (i.e. read preference mode "primary"). 默认情况下,应用程序将其读取操作定向到副本集中的primary
成员(即读取首选项模式“primary”)。But, clients can specify a read preference to send read operations to secondaries.但是,客户端可以指定一个读取首选项,以便将读取操作发送到辅助设备。
Read preference consists of the read preference mode and optionally, a tag set, the 读取首选项包括读取首选项模式和可选的标记集、maxStalenessSeconds
option, and the hedged read option. maxStalenessSeconds
选项和对冲读取选项。Hedged read option is available for MongoDB 4.4+ sharded clusters for reads that use non-对冲读取选项可用于MongoDB 4.4+分片化集群,用于使用非primary
read preference.primary
读取首选项的读取。
The following table lists a brief summary of the read preference modes:下表列出了读取首选项模式的简要摘要:
Starting in version 4.4, non-从4.4版开始,非primary
read preference modes support hedged read on sharded clusters.primary
读取偏好模式支持分片集群上的对冲读取。
primary |
|
primaryPreferred |
|
secondary |
|
secondaryPreferred |
|
nearest |
|
For detailed description of the read preference modes, see Read Preference Modes.有关读取首选项模式的详细说明,请参阅读取首选项。
primary
may return stale data because secondaries replicate operations from the primary in an asynchronous process. primary
外的所有读取首选项模式都可能返回过时数据,因为secondaries在异步进程中从primary复制操作。[1] primary
mode.primary
模式,请确保应用程序可以容忍过时数据。"majority"
read concern and write operations with "majority"
write concern hold across all members of the MongoDB deployment."majority"
读取关注的读操作和具有"majority"
写入关注的写操作,MongoDB部署的所有成员都具有因果一致性会话提供的因果一致性保证。primary
All read operations use only the current replica set primary. 所有读取操作仅使用当前副本集主primary。[1] This is the default read mode. If the primary is unavailable, read operations produce an error or throw an exception.这是默认的读取模式。如果主节点不可用,则读取操作会产生错误或引发异常。
The primary
read preference mode is not compatible with read preference modes that use tag sets or maxStalenessSeconds. primary
读取首选项模式与使用标记集或maxStalenessSeconds
的读取首选项不兼容。If you specify tag sets or a 如果使用maxStalenessSeconds
value with primary
, the driver will produce an error.primary
指定标记集或maxStalenessSeconds
值,驱动程序将产生错误。
Multi-document transactions that contain read operations must use read preference 包含读取操作的多文档事务必须使用读取首选项primary
. primary
。All operations in a given transaction must route to the same member.给定事务中的所有操作都必须路由到同一成员。
primaryPreferred
In most situations, operations read from the primary member of the set. 在大多数情况下,操作从集合的primary成员读取。However, if the primary is unavailable, as is the case during failover situations, operations read from secondary members that satisfy the read preference's 但是,如果主成员不可用(如故障切换情况下的情况),则从满足读取首选项maxStalenessSeconds
and tag sets.maxStalenessSeconds
和标记集的secondary成员读取操作。
When the 当primaryPreferred
read preference includes a maxStalenessSeconds value and there is no primary from which to read, the client estimates how stale each secondary is by comparing the secondary's last write to that of the secondary with the most recent write. primaryPreferred
读取首选项包含maxStalenessSeconds
值,并且没有要从中读取的主项时,客户端通过将次项的上次写入与次项的最新写入进行比较来估计每个次项的过时程度。The client then directs the read operation to a secondary whose estimated lag is less than or equal to 然后,客户端将读取操作定向到估计滞后小于或等于maxStalenessSeconds
.maxStalenessSeconds
的辅助设备。
When the read preference includes a tag set (i.e. a list of tag specifications) and there is no primary from which to read, the client attempts to find secondary members with matching tags (trying the tag specifications in order until a match is found). 当读取首选项包括标签集(即标签规范列表)并且没有要读取的主要成员时,客户端尝试查找具有匹配标签的次要成员(按顺序尝试标签规范,直到找到匹配)。If matching secondaries are found, the client selects a random secondary from the nearest group of matching secondaries. 如果找到匹配的辅助服务器,则客户端从最近的匹配辅助服务器组中随机选择一个辅助服务器。If no secondaries have matching tags, the read operation produces an error.如果没有辅助设备具有匹配的标记,则读取操作将产生错误。
When the read preference includes a 当读取首选项包含maxStalenessSeconds
value and a tag set, the client filters by staleness first and then by the specified tags.maxStalenessSeconds
值和标记集时,客户端会先按过时性进行筛选,然后按指定的标记进行筛选。
Read operations using the 使用primaryPreferred
mode may return stale data. Use the maxStalenessSeconds
option to avoid reading from secondaries that the client estimates are overly stale.primaryPreferred
模式的读取操作可能会返回过时的数据。使用maxStalenessSeconds
选项可以避免从辅助设备读取客户机估计的数据过于陈旧。
Starting in version 4.4, 从4.4版开始,primaryPreferred
supports hedged reads on sharded clusters.primaryPreferred
支持分片集群上的对冲读取。
secondary
Operations read only from the secondary members of the set. 仅从集合的secondary成员读取的操作。If no secondaries are available, then this read operation produces an error or exception.如果没有可用的辅助设备,则此读取操作会产生错误或异常。
Most replica sets have at least one secondary, but there are situations where there may be no available secondary. 大多数副本集至少有一个辅助副本,但在某些情况下可能没有可用的辅助副本。For example, a replica set with a primary, a secondary, and an arbiter may not have any secondaries if a member is in recovering state or unavailable.例如,如果成员处于恢复状态或不可用,则具有primary、secondary和仲裁器的副本集可能没有任何辅助副本集。
When the 当secondary
read preference includes a maxStalenessSeconds value, the client estimates how stale each secondary is by comparing the secondary's last write to that of the primary. secondary
读取首选项包含maxStalenessSeconds
值时,客户端通过将辅助读取的上次写入与主读取的写入进行比较来估计每个辅助读取的过时程度。The client then directs the read operation to a secondary whose estimated lag is less than or equal to 然后,客户端将读取操作定向到估计滞后小于或等于maxStalenessSeconds
. maxStalenessSeconds
的辅助设备。If there is no primary, the client uses the secondary with the most recent write for the comparison.如果没有主文件,则客户端使用具有最新写入的次文件进行比较。
When the read preference includes a tag set (i.e. a list of tag specifications), the client attempts to find secondary members with matching tags (trying the tag specifications in order until a match is found). 当读取偏好包括标签集(即标签规范列表)时,客户端尝试查找具有匹配标签的次要成员(按顺序尝试标签规范,直到找到匹配)。If matching secondaries are found, the client selects a random secondary from the nearest group of matching secondaries. 如果找到匹配的辅助服务器,则客户端从最近的匹配辅助服务器组中随机选择一个辅助服务器。If no secondaries have matching tags, the read operation produces an error.如果没有辅助设备具有匹配的标记,则读取操作将产生错误。
When the read preference includes a 当读取首选项包含maxStalenessSeconds
value and a tag set, the client filters by staleness first and then by the specified tags.maxStalenessSeconds
值和标记集时,客户端会先按过时性进行筛选,然后按指定的标记进行筛选。
Read operations using the 使用secondary
mode may return stale data. secondary
模式的读取操作可能会返回过时的数据。Use the 使用maxStalenessSeconds
option to avoid reading from secondaries that the client estimates are overly stale.maxStalenessSeconds
选项可以避免从辅助设备读取客户机估计的数据过于陈旧。
Starting in version 4.4, 从4.4版开始,secondary
supports hedged reads on sharded clusters.secondary
支持分片集群上的对冲读取。
secondaryPreferred
In most situations, operations read from secondary members, but in situations where the set consists of a single primary (and no other members), the read operation will use the replica set's primary.在大多数情况下,操作从secondary成员读取,但在集由单个primary(没有其他成员)组成的情况下,读取操作将使用副本集的主成员。
When the 当secondaryPreferred
read preference includes a maxStalenessSeconds value, the client estimates how stale each secondary is by comparing the secondary's last write to that of the primary. secondaryPreferred
读取首选项包含maxStalenessSeconds
值时,客户端通过将次文件的上次写入与主文件的写入进行比较来估计每个次文件的过时程度。The client then directs the read operation to a secondary whose estimated lag is less than or equal to 然后,客户端将读取操作定向到估计滞后小于或等于maxStalenessSeconds
. maxStalenessSeconds
的辅助设备。If there is no primary, the client uses the secondary with the most recent write for the comparison. 如果没有主文件,则客户端使用具有最新写入的次文件进行比较。If there are no secondaries with estimated lag less than or equal to 如果没有估计延迟小于或等于maxStalenessSeconds
, the client directs the read operation to the replica set's primary.maxStalenessSeconds
的辅助副本,则客户端将读取操作定向到副本集的主副本集。
When the read preference includes a tag set (i.e. a list of tag specifications), the client attempts to find secondary members with matching tags (trying the tag specifications in order until a match is found). 当读取偏好包括标签集(即标签规范列表)时,客户端尝试查找具有匹配标签的次要成员(按顺序尝试标签规范,直到找到匹配)。If matching secondaries are found, the client selects a random secondary from the nearest group of matching secondaries. 如果找到匹配的辅助服务器,则客户端从最近的匹配辅助服务器组中随机选择一个辅助服务器。If no secondaries have matching tags, the client ignores tags and reads from the primary.如果没有辅助服务器具有匹配的标记,则客户端将忽略标记并从主服务器读取。
When the read preference includes a 当读取首选项包含maxStalenessSeconds
value and a tag set, the client filters by staleness first and then by the specified tags.maxStalenessSeconds
值和标记集时,客户端会先按过时性进行筛选,然后按指定的标记进行筛选。
Read operations using the 使用secondaryPreferred
mode may return stale data. secondaryPreferred
模式的读取操作可能会返回过时的数据。Use the 使用maxStalenessSeconds
option to avoid reading from secondaries that the client estimates are overly stale.maxStalenessSeconds
选项可以避免从辅助设备读取客户机估计的数据过于陈旧。
Starting in version 4.4, 从4.4版开始,secondaryPreferred
supports hedged reads on sharded clusters.secondaryPreferred
支持分片集群上的对冲读取。
nearest
The driver reads from a member whose network latency falls within the acceptable latency window. 驱动程序读取网络延迟在可接受延迟窗口内的成员。Reads in the 在路由读取操作时,nearest
mode do not consider whether a member is a primary or secondary when routing read operations: primaries and secondaries are treated equivalently.nearest
模式下的读取不考虑成员是primary还是secondary: primary和secondary被同等对待。
Set this mode to minimize the effect of network latency on read operations without preference for current or stale data.设置此模式以最小化网络延迟对读取操作的影响,而不首选当前数据或过时数据。
When the read preference includes a maxStalenessSeconds value, the client estimates how stale each secondary is by comparing the secondary's last write to that of the primary, if available, or to the secondary with the most recent write if there is no primary. 当读取首选项包含maxStalenessSeconds
值时,客户端通过将次文件的最后一次写入与主文件的写入(如果可用)进行比较,或者将次文件与最近一次写入(如果没有主文件)进行比较来估计每个次文件的过时程度。The client will then filter out any secondary whose estimated lag is greater than 然后,客户端将筛选掉任何估计延迟大于maxStalenessSeconds
and randomly direct the read to a remaining member (primary or secondary) whose network latency falls within the acceptable latency window.maxStalenessSeconds
的次要成员,并将读取随机引导到网络延迟在可接受的延迟窗口内的剩余成员(主要或次要)。
If you specify a tag set, the client attempts to find a replica set member that matches the specified tag sets and directs reads to an arbitrary member from among the nearest group.如果指定标记集,则客户端将尝试查找与指定标记集匹配的副本集成员,并将读取指向最近组中的任意成员。
When the read preference includes a 当读取首选项包含maxStalenessSeconds
value and a tag set, the client filters by staleness first and then by the specified tags. maxStalenessSeconds
值和标记集时,客户端会先按过时性进行筛选,然后按指定的标记进行筛选。From the remaining 然后,客户端从剩余的mongod
instances, the client then randomly directs the read to an instance that falls within the acceptable latency window. mongod
实例中随机地将读取引导到一个在可接受的延迟窗口内的实例。The read preference member selection documentation describes the process in detail.阅读首选项成员选择文档详细描述了该过程。
Read operations using the 使用nearest
mode may return stale data. nearest
模式的读取操作可能会返回过时的数据。Use the 使用maxStalenessSeconds
option to avoid reading from secondaries that the client estimates are overly stale.maxStalenessSeconds
选项可以避免从辅助设备读取客户机估计的数据过于陈旧。
Starting in version 4.4, read preference 从4.4版开始,默认情况下,nearest
, by default, specifies the use of hedged reads for reads on a sharded cluster.nearest
读取首选项指定在分片化集群上使用对冲读取。
To learn about use cases for specific read preference settings, see Read Preference Use Cases.要了解特定读取首选项设置的用例,请参阅读取首选项用例。
When using a MongoDB driver, you can specify the read preference using the driver's read preference API. 使用MongoDB驱动程序时,可以使用驱动程序的读取首选项API指定读取首选项。See the driver API documentation. You can also set the read preference (except for the hedged read option) when connecting to the replica set or sharded cluster. 请参阅驱动程序API文档。您还可以在连接到副本集或分片集群时设置读取首选项(除了对冲读取选项)。For an example, see connection string.有关示例,请参阅连接字符串。
For a given read preference, the MongoDB drivers use the same member selection logic.对于给定的读取偏好,MongoDB驱动程序使用相同的成员选择逻辑。
When using 使用mongosh
, see cursor.readPref()
and Mongo.setReadPref()
.mongosh
时,请参阅cursor.readPref()
和Mongo.setReadPref()
。
Multi-document transactions that contain read operations must use read preference 包含读取操作的多文档事务必须使用读取首选项primary
. primary
。All operations in a given transaction must route to the same member.给定事务中的所有操作都必须路由到同一成员。
For aggregation pipeline operations that include the 对于包含$out
or $merge
stages, the pipeline runs on the primary member regardless of read preference setting.$out
或$merge
阶段的聚合管道操作,无论读取首选项设置如何,管道都在primary成员上运行。
For 对于mapReduce
operations, only "inline" mapReduce
operations that do not write data support read preference. mapReduce
操作,只有不写入数据的“内联”mapReduce
运算支持读取首选项。Otherwise, 否则,mapReduce
operations run on the primary member.mapReduce
操作将在primary成员上运行。
[1] | (1, 2) { 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 ,但对前一个主机的新写入最终会回滚。 |