Database Manual / Reference / mongosh Methods / Connections

Mongo.setReadPref() (mongosh method方法)

Definition定义

Mongo.setReadPref(mode, tagSet)

Call the setReadPref() method on a Mongo connection object to control how the client will route all queries to members of the replica set. 在Mongo连接对象上调用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] To apply a read preference for a specific query or queries, you can apply cursor.readPref() to a cursor before iteration. 要为一个或多个特定查询应用读取首选项,您可以在迭代之前将cursor.readPref()应用于游标。See cursor.readPref() for details.有关详细信息,请参阅cursor.readPref()

Parameters参数

Parameter参数Type类型Description描述
modestring字符串

One of the following read preference modes: primary, primaryPreferred, secondary, secondaryPreferred, or nearest以下读取首选项模式之一:primaryprimaryPreferredsecondarysecondaryPreferrednearest

tagSetarray of documents文档数组

Optional. 可选。A tag set used to target reads to members with the specified tag(s). 用于将读取目标定位到具有指定标记的成员的标记集tagSet is not available if using read preference mode primary.如果使用primary读取首选项模式,则不可用。

For details, see Read Preference Tag Set Lists.有关详细信息,请参阅读取首选项标记集列表

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 datacenter: "B" tag first.在二次选择过程中,MongoDB试图找到datacenter: "B"标签二级成员优先。

  • If found, MongoDB limits the eligible secondaries to those with the datacenter: "B" tag and ignores the remaining tags.如果找到,MongoDB将符合条件的二级服务器限制为具有datacenter: "B"标签的二,并忽略其余标签。
  • If none are found, then, MongoDB tries to find secondary members with the "region": "West" tag.如果没有找到,那么MongoDB会尝试找到带有"region": "West"标签的次要成员。

    • If found, MongoDB limits the eligible secondaries to those with the "region": "West" tag.如果找到,MongoDB将符合条件的二级服务器限制为带有"region": "West"标签的服务器。
    • If none are found, MongoDB uses any eligible secondaries.如果没有找到,MongoDB将使用任何符合条件的二级服务器。

See Order of Tag Matching for details.详见标签匹配顺序