Mongo.setReadPref()

在本页面

Definition

Note

您必须在连接对象上调用Mongo.setReadPref(),然后才能使用该连接检索文档以使用该读取首选项。

[1] 要对一个或多个特定查询应用读取首选项,可以在迭代之前将cursor.readPref()应用于游标。有关详情,请参见cursor.readPref()

Parameters

Parameter Type Description
mode string 下列read preference模式之一:primaryprimaryPreferredsecondarysecondaryPreferrednearest

tagSet 文档数组 可选。 tag set用于将读取定向到具有指定标签的成员。如果使用读取首选项模式primary,则tagSet不可用。
有关详细信息,请参见Tag Set

Mongo.setReadPref()不支持maxStalenessSeconds选项的读取首选项。

Examples

指定读取首选项模式

以下操作将读取首选项mode设置为将读取目标为次要成员。这隐含地允许从第二层读取。

db.getMongo().setReadPref('secondary')

指定读取首选项标签集

要使用特定标签定位次级标签,请包含标签集数组:

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

在辅助选择过程中,MongoDB 尝试首先查找带有datacenter: "B"标签的辅助成员。

有关详情,请参见标签匹配 Sequences

首页