Docs HomeMongoDB Manual

Read Preference Tag Set Lists读取首选项标记集列表

If a replica set member or members are associated with tags, you can specify a tag set list (array of tag sets) in the read preference to target those members.如果一个或多个副本集成员与tags相关联,则可以在读取首选项中指定标记集列表(标记集数组),以针对这些成员。

To configure a member with tags, set members[n].tags to a document that contains the tag name and value pairs. The value of the tags must be a string.若要使用标记配置成员,请将members[n].tags设置为包含标记名称和值对的文档。标记的值必须是字符串。

{ "<tag1>": "<string1>", "<tag2>": "<string2>",... }

Then, you can include a tag set list in the read preference to target tagged members. 然后,您可以在读取首选项中包含一个标记集列表,以针对已标记成员。A tag set list is an array of tag sets, where each tag set contains one or more tag/value pairs.标记集列表是标记集的数组,其中每个标记集包含一个或多个标记/值对。

[ { "<tag1>": "<string1>", "<tag2>": "<string2>",... }, ... ]

To find replica set members, MongoDB tries each document in succession until a match is found. 为了找到副本集成员,MongoDB会依次尝试每个文档,直到找到匹配的文档。See Order of Tag Matching for details.有关详细信息,请参阅标记匹配顺序

For example, if a secondary member has the following members[n].tags:例如,如果辅助成员具有以下members[n].tags

{ "region": "South", "datacenter": "A" }

Then, the following tag set lists can direct read operations to the aforementioned secondary (or other members with the same tags):然后,以下标记集列表可以将读取操作定向到上述次要成员(或具有相同标记的其他成员):

[ { "region": "South", "datacenter": "A" }, { } ]     // Find members with both tag values. If none are found, read from any eligible member.查找同时具有两个标记值的成员。如果未找到,请从任何符合条件的成员处读取。
[ { "region": "South" }, { "datacenter": "A" }, { } ] // Find members with the specified region tag. 查找具有指定区域标记的成员。Only if not found, then find members with the specified datacenter tag. 只有在找不到的情况下,才能查找具有指定数据中心标记的成员。If none are found, read from any eligible member.如果未找到,请从任何符合条件的成员处读取。
[ { "datacenter": "A" }, { "region": "South" }, { } ] // Find members with the specified datacenter tag. Only if not found, then find members with the specified region tag. 查找具有指定数据中心标记的成员。只有在找不到的情况下,才能查找具有指定区域标记的成员。If none are found, read from any eligible member.如果未找到,请从任何符合条件的成员处读取。
[ { "region": "South" }, { } ] // Find members with the specified region tag value. If none are found, read from any eligible member.查找具有指定区域标记值的成员。如果未找到,请从任何符合条件的成员处读取。
[ { "datacenter": "A" }, { } ] // Find members with the specified datacenter tag value. If none are found, read from any eligible member.查找具有指定数据中心标记值的成员。如果未找到,请从任何符合条件的成员处读取。
[ { } ] // Find any eligible member.查找任何符合条件的成员。

Order of Tag Matching标签匹配顺序

If the tag set list contains multiple documents, MongoDB tries each document in succession until a match is found. 如果标记集列表包含多个文档,MongoDB会连续尝试每个文档,直到找到匹配的文档。Once a match is found, that tag set is used to find all eligible matching members, and the remaining tag sets are ignored. 找到匹配项后,该标记集将用于查找所有符合条件的匹配成员,其余标记集将被忽略。If no members match any of the tag sets, the read operation returns with an error.如果没有成员与任何标记集匹配,则读取操作将返回一个错误。

Tip

To avoid an error if no members match any of the tag specifications, you can add an empty document { } as the last element of the tag set list to read from any eligible member.如果没有成员与任何标记规范匹配,为了避免出现错误,可以添加一个空文档{ }作为标记集列表的最后一个元素,以便从任何符合条件的成员中读取。

For example, consider the following tag set list with three tag sets:例如,考虑以下具有三个标记集的标记集列表:

[ { "region": "South", "datacenter": "A" },  { "rack": "rack-1" }, { } ]

First, MongoDB tries to find members tagged with both "region": "South" and "datacenter": "A".首先,MongoDB试图找到同时标记有"region": "South""datacenter": "A"的成员。

{ "region": "South", "datacenter": "A" }
  • If a member is found, the remaining tag sets are not considered. 如果找到一个成员,则不考虑剩余的标记集。Instead, MongoDB uses this tag set to find all eligible members.相反,MongoDB使用这个标签集来查找所有符合条件的成员。
  • Else, MongoDB tries to find members with the tags specified in the second document否则,MongoDB会尝试使用第二个文档中指定的标签来查找成员

    { "rack": "rack-1" }
    • If a member is found tagged, the remaining tag set is not considered. 如果发现某个成员已标记,则不考虑剩余的标记集。Instead, MongoDB uses this tag set to find all eligible members.相反,MongoDB使用这个标签集来查找所有符合条件的成员。
    • Else, the third document is considered.否则,将考虑第三份文件。

      { }

      The empty document matches any eligible member.空文档匹配任何符合条件的成员。

Tag Set List and Read Preference Modes标记集列表和读取首选项模式

Tags are not compatible with mode primary, and in general, only apply when selecting a secondary member of a set for a read operation. 标记与primary模式不兼容,通常只有在为读取操作选择集合的辅助成员时才适用。However, the nearest read mode, when combined with a tag set list, selects the matching member with the lowest network latency. 然而,当与标签集列表组合时,nearest读取模式会选择具有最低网络延迟的匹配成员。This member may be a primary or secondary.此成员可以是主要成员,也可以是次要成员。

Mode模式Notes备注
primaryPreferredSpecified tag set list only applies if selecting eligible secondaries.指定的标记集列表仅适用于选择符合条件的辅助设备的情况。
secondarySpecified tag set list always applies.指定的标记集列表始终适用。
secondaryPreferredSpecified tag set list only applies if selecting eligible secondaries.指定的标记集列表仅适用于选择符合条件的辅助设备的情况。
nearestSpecified tag set list applies whether selecting either primary or eligible secondaries.指定的标记集列表适用于选择主辅助还是符合条件的辅助。

For information on the interaction between the modes and tag set lists, refer to the specific read preference mode documentation.有关模式和标记集列表之间交互的信息,请参阅特定的读取首选项模式文档

For information on configuring tag set lists, see the Configure Replica Set Tag Sets tutorial.有关配置标记集列表的信息,请参阅配置副本集标记集教程