cursor.readPref()
On this page本页内容
Definition定义
cursor.readPref(mode, tagSet, hedgeOptions)
- Important
mongosh Method
This page documents a
mongosh
method. This is not the documentation for a language-specific driver, such as Node.js.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()
附加到游标,以控制客户端如何将查询路由到副本集的成员。NoteYou must apply在从数据库检索任何文档之前,必须将readPref()
to the cursor before retrieving any documents from the database.readPref()
应用于游标。
Parameters参数
mode | string | primary , primaryPreferred , secondary , secondaryPreferred , or nearest |
tagSet | array of documents | tagSet primary .primary ,则不可用。 |
hedgeOptions | document | { enabled: <boolean> } enabled field defaults to true; i.e. specifying an empty document { } is equivalent to specifying { enabled: true } .enabled 字段默认为true ;即,指定一个空文档{ } 相当于指定{ enabled: true } 。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 读取偏好必须启用对冲读取。nearest enables the use of hedged reads on sharded clusters by default; i.e. by default, has { enabled: true } . nearest 允许在分片集群上使用对冲读取;即默认情况下,具有{ enabled: true } 。 |
readPref()
does not support the Read Preference 不支持读取首选项的读取首选项maxStalenessSeconds
option for read preference.maxStalenessSeconds
选项。
Examples实例
Specify Read Preference Mode指定读取首选项模式
The following operation uses the read preference mode to target the read to a secondary member.以下操作使用读取首选项mode
将读取目标定为次要成员。
db.collection.find({ }).readPref( "secondary")
Specify Read Preference Tag Set指定读取首选项标记集
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"
标记的二级成员。
If found, MongoDB limits the eligible secondaries to those with the如果找到,MongoDB会将符合条件的辅助设备限制为具有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"
标签的次要成员。If found, MongoDB limits the eligible secondaries to those with the如果找到,MongoDB将符合条件的二级存储限制为带有"region": "West"
tag."region": "West"
标记的二级。If none are found, MongoDB uses any eligible secondaries.如果没有找到,MongoDB将使用任何符合条件的辅助设备。
See Order of Tag Matching for details.有关详细信息,请参阅标记匹配顺序。
See also: 另请参阅:
Specify Hedged Read指定对冲读取
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
)