On this page本页内容
A replica set member or members can be configured with tags
:
For read operations, you can specify a tag set in the read preferences to help direct read operations to members that have specific tag(s).
For write operations, you can use the tags to create a custom write concern.
If a replica set member or members are associated with tags
, you can specify a tag set in the read preference to target those members. A tag set is an array of documents, where each document contains the tag and value pair(s). The specifications are tried in order until a match is found. Once found, that specification is used to find all eligible matching members.
Note
You cannot specify a tag set when specifying read preference mode primary
.
For example, a replica set has the following replica set configuration (some of the fields have been omitted for brevity):
Connect a mongo
shell to the replica set and use rs.reconfig()
to add tags to the members:
Run rs.conf()
to verify the replica set configuration (some of the fields have been omitted for brevity). The rs.conf()
returns a document similar to the following:
To direct read operations to the secondaries tagged with a particular tag(s), in the mongo
shell connected to the replica set, you can use the readPref()
method to specify the read preference mode and the tag set. For example,
"dc": "east"
and "usage": "production"
, include the following tag set:
"dc":
"east"
, and if not found, to secondaries tagged with "usage": "production"
, include the following tag set:
See also参阅
If a replica set member or members are associated with tags
, you can configure the replica set’s settings.getLastErrorModes
setting to create a custom write concern.
Given a five member replica set with members in two data centers:
VA
tagged dc_va
CA
tagged dc_ca
Connect a mongo
shell to the replica set and use rs.reconfig()
to add tags to the members:
In the replica set configuration, define a custom write concern in the settings.getLastErrorModes
setting. For example, the following defines the custom write concern MultipleDC
that requires the write to propagate to two members with different
dc_va
tag values and one member with any dc_ca
tag value.
Note
The MultipleDC
write concern is not satisfied if the write propagates to two members with the same "dc_va"
tag. For example, if the write has only propagated to members[0]
and members[4]
, "dc_va": 2
is not satisfied since they have the same tag value "rack1"
.
To use the custom write concern, pass in the write concern name to the w Option in the write concern: