cursor.readPref()

在本页面

Definition

Note

从数据库中检索任何文档之前,必须将readPref()应用于光标。

Parameters

Parameter Type Description
mode string 下列read preference模式之一:primaryprimaryPreferredsecondarysecondaryPreferrednearest
tagSet 文件阵列 可选的。 tag set用于将读取定向到具有指定标签的成员。如果使用primary,则tagSet不可用。


有关详细信息,请参见Tag Set

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

Example

以下操作使用读取首选项模式将读取定向到辅助成员。

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

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

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"标签的辅助成员。

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

首页