Definition定义
Mongo.setReadPref(mode, tagSet)Call the在Mongo连接对象上调用setReadPref()method on aMongoconnection object to control how the client will route all queries to members of the replica set.setReadPref()方法,以控制客户端如何将所有查询路由到副本集的成员。[1]Note
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()。 |
Parameters参数
mode |
| |
tagSet |
|
Mongo.setReadPref() does not support the Read Preference 不支持读取首选项的读取首选项maxStalenessSeconds option for read preference.maxStalenessSeconds选项。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Behavior行为
Mongo.setReadPref() has the following behavior.具有以下行为。
Interaction with db.runCommand()与db.runCommand()的交互
Starting in mongosh 2.0, 从db.runCommand() ignores any global read preferences added through the connection string or by using the Mongo.setReadPref() method. mongosh 2.0开始,db.runCommand()忽略通过连接字符串或使用Mongo.setReadPref()方法添加的任何全局读取首选项。To specify the desired read preference, use the 要指定所需的读取首选项,请在调用options argument when calling db.runCommand().db.runCommand()时使用options参数。
Examples示例
Specify Read Preference Mode指定读取首选项模式
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')
However, if called while connected to the primary using 但是,如果在使用mongosh, the above command does not route queries to a secondary.mongosh连接到主服务器时调用,上述命令不会将查询路由到辅助服务器。
Specify Read Preference Tag Set指定读取首选项标记集
To target secondaries with specific tags, include both the mode and the tagSet array:要针对具有特定标签的次级市场,请同时包含模式和tagSet数组:
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"标签二级成员优先。
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.详见标签匹配顺序。