On this page本页内容
cursor.readPref(mode, tagSet, hedgeOptions)
This is a mongosh
method. This is not the documentation for Node.js
or other programming language specific driver methods.
In most cases, mongosh
methods work the same way as the legacy mongo
shell methods. However, some legacy methods are unavailable in mongosh
.
For the legacy mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
Append 将readPref()
to a cursor to control how the client routes the query to members of the replica set.readPref()
附加到游标,以控制客户端如何将查询路由到副本集的成员。
You must apply 在从数据库检索任何文档之前,必须将readPref()
to the cursor before retrieving any documents from the database.readPref()
应用于游标。
mode | string |
|
tagSet | array of documents |
|
hedgeOptions | document |
|
readPref()
does not support the Read Preference 不支持读取首选项的读取首选项maxStalenessSeconds
option for read preference.maxStalenesSeconds
选项。
The following operation uses the read preference mode to target the read to a secondary member.以下操作使用读取首选项模式将读取定向到次要成员。
db.collection.find({ }).readPref( "secondary")
To target secondaries with specific tags, include both the mode and the tagSet array:要以具有特定标记的次映像为目标,请同时包含mode
和tagSet
数组:
db.collection.find({ }).readPref( "secondary", [ { "datacenter": "B" }, // First, try matching by the datacenter tag { "region": "West"}, // If not found, then try matching by the region tag { } // If not found, then use the empty document to match all eligible members ] )
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"
标记的辅助设备,并忽略其余标记。If none are found, then, MongoDB tries to find secondary members with the 如果未找到任何成员,则MongoDB会尝试查找带有"region": "West"
tag."region": "West"
标记的次要成员。
"region": "West"
tag."region": "West"
标记的次映像。See Order of Tag Matching for details.有关详细信息,请参阅标签匹配顺序。
Starting in MongoDB 4.4 for sharded clusters, you can enable hedged reads for non-primary read preferences. 从MongoDB 4.4中的分片集群开始,您可以为非primary读取首选项启用对冲读取。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
必须<启用对冲读取支持(默认设置),非primary
读取首选项必须启用对冲读取。
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+分片集群上瞄准二级,请同时包括mode
和hedgeOptions
,如以下示例所示:
Without a tag set没有标记集
db.collection.find({ }).readPref( "secondary", // mode null, // tag set { enabled: true } // hedge options )
With a tag set带有标记集
db.collection.find({ }).readPref( "secondary", // mode [ { "datacenter": "B" }, { } ], // tag set { enabled: true } // hedge options )