On this page本页内容
cursor.
readPref
(mode, tagSet, hedgeOptions)¶mongo
Shell Method
This page documents the 本页记录了mongo
shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. mongo
shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。
Append 将readPref()
to a cursor to control how the client routes the query to members of the replica set.readPref()
附加到游标,以控制客户端如何将查询路由到副本集的成员。
Note
You must apply 在从数据库检索任何文档之前,必须对游标应用readPref()
to the cursor before retrieving any documents from the database.readPref()
。
mode | string |
|
tagSet | array of documents |
|
hedgeOptions | document |
|
cursor.setReadPref()
does not support the Read Preference maxStalenessSeconds option for read preference.不支持读取首选项的读取首选项maxStalenessSeconds
。
The following operation uses the read preference mode to target the read to a secondary member.以下操作使用读取首选项模式将读取指向辅助成员。
To target secondaries with specific tags, include both the mode and the tagSet array:要以具有特定标记的辅助设备为目标,请包括模式和标记集数组:
During the secondary selection process, MongoDB tries to find secondary members with the 在二级选择过程中,MongoDB尝试首先使用datacenter: "B"
tag first.datacenter: "B"
标记查找二级成员。
datacenter: "B"
tag and ignores the remaining tags.datacenter: "B"
标记的二级数据库,并忽略其余的标记。"region": "West"
tag."region": "West"
标记的次要成员。"region": "West"
tag."region": "West"
标签的供应商。See Order of Tag Matching for details.详见标签匹配顺序。
See also参阅
Starting in MongoDB 4.4 for sharded clusters, you can enable hedged reads for non-primary read preferences. 从针对分片集群的MongoDB 4.4开始,您可以为非主要读取首选项启用模糊读取。To use hedged reads, the 要使用模糊读取,mongos
must have enabled support
for hedged reads (the default) and the non-primary
read preferences must enable the use of hedged reads.mongos
必须启用对模糊读取的支持(默认),非主读取首选项必须启用模糊读取。
To target secondaries on 4.4+ sharded cluster using hedged reads, include both the mode and the hedgeOptions, as in the following examples:要使用对冲读取以4.4+分片集群上的二级数据为目标,请包括模式和对冲选项,如以下示例所示: