cursor.readPref()

On this page本页内容

Definition定义

cursor.readPref(mode, tagSet, hedgeOptions)
Important重要
mongosh Method

This is a mongosh method. This is not the documentation for Node.js or other programming language specific driver methods.

In most cases, mongosh methods work the same way as the legacy mongo shell methods. However, some legacy methods are unavailable in mongosh.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

For MongoDB API drivers, refer to the language specific MongoDB driver documentation.

Append readPref() to a cursor to control how the client routes the query to members of the replica set.readPref()附加到游标,以控制客户端如何将查询路由到副本集的成员。

Note注意

You must apply readPref() to the cursor before retrieving any documents from the database.在从数据库检索任何文档之前,必须将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 primary.如果使用primary,则tagSet不可用。

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

hedgeOptionsdocument

Optional. 可选。A document that specifies whether to enable the use of hedged reads.指定是否启用对冲读取的文件。

{ enabled: <boolean> }

The enabled field defaults to true; i.e. specifying an empty document { } is equivalent to specifying { enabled: true }.enabled字段默认为true;例如,指定空文档{}等同于指定{ enabled: true }

Hedged reads are available starting in MongoDB 4.4 for sharded clusters. 对于分片集群,从MongoDB 4.4开始可以使用对冲读取。To use hedged reads, the mongos must have enabled support for hedged reads (the default) and the non-primary read preferences must enable the use of hedged reads.要使用对冲读取,mongos必须启用对冲读取支持(默认设置),非primary读取首选项必须启用对冲阅读。

Read preference nearest enables the use of hedged reads on sharded clusters by default; i.e. by default, has { enabled: true }.读取首选项nearest默认情况下允许在分片集群上使用对冲读取;即,默认情况下,具有{enabled:true}

New in version 4.4.在版本4.4中新增

readPref() does not support the Read Preference maxStalenessSeconds option for read preference.不支持读取首选项的读取首选项maxStalenesSeconds选项

Examples示例

Specify Read Preference Mode指定读取首选项模式

The following operation uses the read preference mode to target the read to a secondary member.以下操作使用读取首选项模式将读取定向到次要成员。

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

Specify Read Preference Tag Set指定读取首选项标记集

To target secondaries with specific tags, include both the mode and the tagSet array:要以具有特定标记的次映像为目标,请同时包含modetagSet数组:

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

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.有关详细信息,请参阅标签匹配顺序

Specify Hedged Read指定对冲读取

Starting in MongoDB 4.4 for sharded clusters, you can enable hedged reads for non-primary read preferences. 从MongoDB 4.4中的分片集群开始,您可以为非primary读取首选项启用对冲读取To use hedged reads, the mongos must have enabled support for hedged reads (the default) and the non-primary read preferences must enable the use of hedged reads.要使用对冲读取,mongos必须<启用对冲读取支持(默认设置),非primary读取首选项必须启用对冲读取。

To target secondaries on 4.4+ sharded cluster using hedged reads, include both the mode and the hedgeOptions, as in the following examples:要使用对冲读取在4.4+分片集群上瞄准二级,请同时包括modehedgeOptions,如以下示例所示:

  • Without a tag set没有标记集

    db.collection.find({ }).readPref(
       "secondary",
                // mode
       null,
                       // tag set
       { enabled: true }
           // hedge options
    )
  • With a tag set带有标记集

    db.collection.find({ }).readPref(
       "secondary",
                          // mode
       [ { "datacenter": "B" },  { } ],  // tag set
       { enabled: true }
                     // hedge options
    )
←  cursor.readConcern()cursor.returnKey() →