On this page本页内容
Mongo.setReadPref(mode, tagSet, hedgeOptions)
Call the 对Mongo连接对象调用setReadPref()
method on a Mongo
connection object to control how the client will route all queries to members of the replica set. setReadPref()
方法,以控制客户端如何将所有查询路由到副本集的成员。[1]
You must call 在使用该连接检索文档之前,必须对连接对象调用Mongo.setReadPref()
on the connection object before retrieving documents using that connection to use that read preference.Mongo.setReadPref()
才能使用该读取首选项。
[1] | cursor.readPref() to a cursor before iteration. cursor.readPref() 应用于游标。cursor.readPref() for details.cursor.readPref() 。 |
mode | string |
|
tagSet | array of documents |
|
hedgeOptions | document |
|
Mongo.setReadPref()
does not support the Read Preference 不支持读取首选项的读取首选项maxStalenessSeconds
option for read preference.maxStalenesSeconds
选项。
The following operation sets the read preference mode to target the read to a secondary member. 以下操作将读取首选项模式设置为将读取定向到次要成员。This implicitly allows reads from secondaries.这隐式允许从辅助文件读取。
db.getMongo().setReadPref('secondary')
To target secondaries with specific tags, include both the mode and the tagSet array:要以具有特定标记的次映像为目标,请同时包含模式和标记集数组:
db.getMongo().setReadPref( "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中的分片集群开始,您可以为非主读取首选项启用对冲读取。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+分片集群上瞄准二级,请同时包括模式和对冲选项,如以下示例所示:
Without a tag set没有标记集
db.getMongo().setReadPref( "secondary", // mode null, // tag set { enabled: true } // hedge options )
With a tag set带有标记集
db.getMongo().setReadPref( "secondary", // mode [ { "datacenter": "B" }, { } ], // tag set { enabled: true } // hedge options )