On this page本页内容
sh.
updateZoneKeyRange
(namespace, minimum, maximum, zone)¶New in version 3.4.版本3.4中的新功能。
Associates a range of shard key values with a zone.
Starting in MongoDB 4.0.2, you can run updateZoneKeyRange
database command and its helpers sh.updateZoneKeyRange()
and sh.addTagRange()
on an unsharded collection or a non-existing collection.
sh.updateZoneKeyRange()
takes the following arguments:
namespace |
string | The namespace of the sharded collection associate with the The collection must be sharded for the operation to succeed. |
minimum |
document | The inclusive lower bound of the range of shard key values. Specify each field of the shard key in the form of |
maximum |
document | The exclusive upper bound of the range of shard key values. Specify each field of the shard key in the form of |
zone |
string | The name of the zone to associate with the range of shard key values bounded by minimum and maximum . |
Only issue sh.updateZoneKeyRange()
when connected to a mongos
instance.
You cannot create a range of shard key values whose lower and upper boundaries overlap with an existing range for the sharded collection. For example, given an existing range of 1
to 10
, you cannot create a new range of 5
to 20
, as the new range would overlap with the existing range.
A zone can have multiple ranges of data associated with it, but a range can at most be associated with a single zone.
See the zone manual page for more information on zones in sharded clusters.
If you are considering performing zone sharding on an empty or non-existent collection, use sh.updateZoneKeyRange()
to create the zones and zone ranges before sharding the collection (since 4.0.2). Starting in version 4.0.3, creating zones and zone ranges on empty or non-existing collections allows MongoDB to optimize the initial chunk creation and distribution process when sharding the collection. This optimized process supports faster setup of zoned sharding with less balancer overhead than creating zones after sharding. The balancer performs all chunk management after the optimized initial chunk creation and distribution.
For an example of defining zones and zone ranges for initial chunk distribution, see Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection.
Starting in version 4.4, MongoDB supports sharding collections on compound hashed indexes. MongoDB can perform optimized initial chunk creation and distribution when sharding the empty or non-existing collection on a compound hashed shard key.
If the hashed field is the prefix of the shard key (i.e. the first field in the shard key), all of the following must be true for MongoDB to perform initial chunk creation and distribution:
MinKey
for all lower-bound fields and MaxKey
for all upper-bound fields.sh.shardCollection()
specifies the presplitHashedZones: true option.If the hashed field is not the prefix of the shard key (i.e. the shard key has one or more non-hashed leading fields), all of the following must be true for MongoDB to perform initial chunk creation and distribution:
MinKey
for the hashed field and all subsequent fields.sh.shardCollection()
specifies the presplitHashedZones: true option.For a more complete example of defining zones and zone ranges for initial chunk distribution on a compound hashed shard key, see Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection.
See also参阅
After associating a range to a zone, the balancer must first run in order to migrate any chunks whose ranges are covered by the zone to shards inside of that zone. Until balancing completes, some chunks may reside on the wrong shard given the configured zones for the sharded cluster. See Balancer for more information.
See the sharded cluster balancer manual page for more information on how migrations work in a sharded cluster.
Zone ranges are always inclusive of the lower boundary and exclusive of the upper boundary.
Starting in MongoDB 4.0.2, dropping a collection deletes its associated zone/tag ranges.
In earlier versions, MongoDB does not remove the tag associations for a dropped collection, and if you later create a new collection with the same name, the old tag associations will apply to the new collection.
For sharded clusters running with authentication, you must authenticate as either:
config
database:
or, alternatively,
enableSharding
on the cluster resource (available starting in version 4.2.2, 4.0.14, 3.6.16).The clusterAdmin
or clusterManager
built-in roles have the appropriate permissions for issuing sh.updateZoneKeyRange()
. See the documentation page for Role-Based Access Control for more information.
Given a sharded collection exampledb.collection
with a shard key of { a : 1 }
, the following operation creates a range with a lower bound of 1
and an upper bound of 10
on the alpha
zone:
The following operation removes the previously created range by passing null
to the zone
field.
The min
and max
must match exactly the bounds of the target range. The following operation attempts to remove the previously created range, but specifies { a : 0 }
as the min
bound:
While the range of { a : 0 }
and { a : 10 }
encompasses the existing range, it is not an exact match and therefore updateZoneKeyRange
does not remove anything.
Given a sharded collection exampledb.collection
with a shard key of { a : 1, b : 1 }
, the following operation creates a range covering the lower bound of { a: 1, b : 1 }
and an upper bound of { a : 10, b : 10}
and associates it with the alpha
zone:
If you wanted to create a range on values of b
only, you must specify the entire range of a
when creating ranges. For example, the following operations create two ranges on b
and associates them to the beta
zone.
Note
The previously defined range conflicts with the ranges defined in this example. Issue the sh.removeRangeFromZone()
operation to remove an existing conflicting range.
MinKey
always compares as lower than every other possible value, while MaxKey
always compares as higher than every other possible value. Using these special values for the lower and upper bounds of the range captures the entire possible value space of a
.
Starting in version 4.2, creating zones and zone ranges on empty or non-existing collections allows MongoDB to optimize the initial chunk creation and distribution process when sharding the collection. This optimized process supports faster setup of zoned sharding with less balancer overhead than creating zones after sharding. The balancer performs all chunk management after the optimized initial chunk creation and distribution. See Initial Chunk Distribution with Compound Hashed Shard Keys for more information.
Each of the following tabs contains an example specific to the described shard key type:
Note
The content in this tab only applies to single-field or compound shard keys without a hashed field.
For example, { "zip" : 1 }
or { "zip" : 1, "account" : 1}
Use sh.addShardToZone()
to create the zones:
Use sh.updateZoneKeyRange()
to create the ranges for the empty contacts
collection in the exampledb
database:
Skip this step if you already enabled sharding on the database.
Use sh.enableSharding()
to enable sharding for the database:
Note
If the collection does not exist, the sharding operation creates the collection.
If the collection is empty and no index exists to support the shard key, the sharding operation creates the index.
Use sh.shardCollection()
to shard the collection contacts
:
To see the created chunks and distribution, run the sh.status()
operation:
The method returns:
For the collection, sharding operation created 5 chunks (two chunks that correspond to the zone ranges and the other three to cover all other values) across shardA and shardB.
Note
The content in this tab only applies to compound hashed shard keys where the hashed field is the prefix of the shard key (i.e. the first field in the shard key is hashed).
For example, { "_id" : "hashed", "facility" : 1 }
Starting in version 4.4, MongoDB supports sharding collections on compound hashed indexes. When sharding on a compound hashed shard key, MongoDB can perform optimized initial chunk creation and distribution on the empty or non-existing collection only if the defined zone ranges meet additional requirements.
Consider an empty collection examples.metrics
which will store analytics from one of two manufacturing facilities. The planned shard key is { "_id" : "hashed", "facility" : 1}
, where the hashed field is the shard key prefix.
The planned shard key is { "_id" : "hashed", "facility" : 1 }
. Since the hashed field is the prefix (i.e. the first field in the shard key), create a single zone using sh.addShardToZone()
:
Initial chunk distribution on a compound hashed shard key with a hashed prefix requires a single zone range with MinKey
for all lower-bound fields and MaxKey
for all upper-bound fields.
Use sh.updateZoneKeyRange()
to create a single range:
Skip this step if you already enabled sharding on the database.
Use sh.enableSharding()
to enable sharding for the database:
Note
If the collection does not exist, the sharding operation creates the collection.
If the collection is empty and no index exists to support the shard key, the sharding operation creates the index.
Use sh.shardCollection()
with presplitHashedZones: true to shard the collection and perform initial chunk creation and distribution:
To see the created chunks and distribution, run the sh.status()
operation:
The output resembles the following (content omitted for readability):
The sharding operation produced 4 chunks in total. Two chunks correspond to the absolute lower and upper bounds. One zone was created on shardA
and shardB
corresponding to FacilityAlpha
. The zone was subdivided into 2 chunks using the hashed field.
Note
The content in this tab only applies to compound hashed shard keys where the hashed field is not the prefix of the shard key (i.e. the first field in the shard key is not hashed).
For example, { "facility" : 1, "_id" : "hashed" }
Starting in version 4.4, MongoDB supports sharding collections on compound hashed indexes. When sharding on a compound hashed shard key, MongoDB can perform optimized initial chunk creation and distribution on the empty or non-existing collection only if the defined zone ranges meet additional requirements.
Consider an empty collection examples.metrics
which will store analytics from one of two manufacturing facilities. The planned shard key is { "facility" : 1, "_id" : "hashed" }
, where the hashed field is not the shard key prefix.
facility
field stores the name of the facility:
"FacilityAlpha"
or "FacilityBaker"
. The collection requires zone ranges on facility
to help isolate data for each facility to specific shards._id
field compensates for the low-cardinality of the facility
field. Hashing compensates for the monotonically-increasing nature of the _id
field.Use the sh.addShardToZone()
command to create the zones.
The planned shard key is {"facility" : 1, "_id" : "hashed"}
. The facility
field has two possible values: FacilityAlpha
and FacilityBaker
.
Initial chunk distribution on a compound hashed shard key where the hashed field is not the prefix requires one zone range for each combination of distinct prefix field values (i.e. all fields preceding the hashed field). Since facility
has two distinct prefix values, the collection requires exactly two zone ranges that cover those values.
MinKey
for all non-prefix fields.Use sh.updateZoneKeyRange()
to create the range for "facility": "FacilityAlpha"
:
"facilty" : "FacilityAlpha"
and all possible values of _id
.Use sh.updateZoneKeyRange()
to create the range for "facility": "FacilityBaker"
:
facility
, the initial chunk distribution logic relies on the assumption that no other distinct values for facility
exist. Since the collection only contains documents where facility
is FacilityAlpha
or FacilityBaker
, this range only covers documents with the distinct shard key prefix value "facility" : "FacilityBaker"
and all possible values of _id
.Skip this step if you already enabled sharding on the database.
Use sh.enableSharding()
to enable sharding for the database:
Note
If the collection does not exist, the sharding operation creates the collection.
If the collection is empty and no index exists to support the shard key, the sharding operation creates the index.
Use sh.shardCollection()
with presplitHashedZones: true to shard the collection and perform initial chunk creation and distribution:
To see the created chunks and distribution, run the sh.status()
operation:
The output resembles the following (content omitted for readability):
The sharding operation produced 6 chunks in total. Two chunks correspond to the absolute lower and upper bounds. Two zones were created, one on shardA
and one on shardB
, corresponding to FacilityAlpha
and FacilityBaker
. Each of these zones has been further subdivided into 2 chunks using the hashed field.
See also参阅