对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

游标.readPref()(mongosh方法)

cursor.readPref(mode, tagSet)

重要

mongosh 方法

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.

如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。

Append readPref() to a cursor to control how the client routes the query to members of the replica set.

注意

You must apply readPref() to the cursor before retrieving any documents from the database.

Parameter
类型
说明

字符串

文档数组

可选。 用于将读取定位到具有指定标签的成员的标签集。 如果使用primary ,则tagSet不可用。

有关详细信息,请参阅读取偏好(read preference)标签集列表。

readPref() does not support the Read Preference maxStalenessSeconds option for read preference.

此方法可用于以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

注意

This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

以下操作使用读取偏好模式将读取定位到从节点成员。

db.collection.find({ }).readPref( "secondary")

To target secondaries with specific tags, include both the mode and the tagSet array:

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
]
)

在从节点选择过程中,MongoDB 会尝试首先找到带有datacenter: "B"标签的从节点。

  • 如果找到,MongoDB 会将符合条件的从节点限制为带有datacenter: "B"标签的从节点,并忽略其余标签。

  • 如果未找到,MongoDB 会尝试查找带有"region": "West"标签的从节点。

    • 如果找到,MongoDB 会将符合条件的从节点限制为带有"region": "West"标签的从节点。

    • 如果未找到,MongoDB 将使用任何符合条件的从节点。

有关详细信息,请参阅标签匹配顺序。

给本页内容打分

在此页面上