Read Preference Tag Sets读取首选项标记集

On this page本页内容

If a replica set member or members are associated with tags, you can specify a tag set (array of tag specification documents) 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. 要使用标记配置成员,请将members[n].tags设置为包含标记名称和值对的文档。The value of the tags must be a string.标记的值必须是字符串。

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

Then, you can include a tag set in the read preference to target tagged members. 然后,您可以在读取首选项中包含标记集,以目标标记成员。A tag set is an array of tag specification documents, where each tag specification document 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 tags sets 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 lists multiple documents, MongoDB tries each document in succession until a match is found. 如果标记集列出了多个文档,MongoDB会连续尝试每个文档,直到找到匹配项。Once a match is found, that tag specification document is used to find all eligible matching members, and the remaining tag specification documents are ignored. 找到匹配项后,将使用该标记规范文档查找所有符合条件的匹配成员,并忽略其余的标记规范文档。If no members match any of the tag specification documents, 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 to read from any eligible member.为了避免在没有成员匹配任何标记规范时出错,可以添加一个空文档{ }作为标记集的最后一个元素,以便从任何符合条件的成员中读取。

For example, consider the following tag set with three tag specification documents:例如,考虑以下带有三个标记规范文档的标记集:

[ { "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 specification documents are not considered. 如果找到成员,则不考虑剩余的标记规范文档。Instead, MongoDB uses this tag specification document 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 specification document is not considered. 如果发现成员已标记,则不考虑剩余的标记规范文档。Instead, MongoDB uses this tag specification document to find all eligible members.相反,MongoDB使用此标记规范文档来查找所有符合条件的成员。
    • Else, the third document is considered.否则,将考虑第三份文件。

      { }

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

Tag Set 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不兼容,通常仅在为读取操作选择集合的secondary成员时适用。However, the nearest read mode, when combined with a tag set, selects the matching member with the lowest network latency. 然而,当与标记集组合时,nearest读取模式选择具有最低网络延迟的匹配成员。This member may be a primary or secondary.此成员可以是主要成员或次要成员。

ModeNotes
primaryPreferredSpecified tag set only applies if selecting eligible secondaries.指定的标记集仅在选择符合条件的辅助设备时适用。
secondarySpecified tag set always applies.指定的标记集始终适用。
secondaryPreferredSpecified tag set only applies if selecting eligible secondaries.指定的标记集仅在选择符合条件的辅助设备时适用。
nearestSpecified tag set applies whether selecting either primary or eligible secondaries.指定的标记集适用于选择主标记集还是合格的辅助标记集。

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

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

←  Read PreferenceRead Preference maxStalenessSeconds →