Docs HomeMongoDB Manual

sh.updateZoneKeyRange()

Definition定义

sh.updateZoneKeyRange(namespace, minimum, maximum, zone)

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.从MongoDB 4.0.2开始,您可以在未排序的集合或不存在的集合上运行updateZoneKeyRange数据库命令及其助手sh.updateZoneKeyRange()sh.addTagRange()

Important

mongosh Method

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

For the database command, see the updateZoneKeyRange command.

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

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

mongo shell v4.4

sh.updateZoneKeyRange() takes the following arguments:采用以下参数:

Parameter参数Type类型Description描述
namespacestringThe namespace of the sharded collection associate with the zone.zone关联的分片集合的命名空间
The collection must be sharded for the operation to succeed. 必须对集合进行分片,操作才能成功。
minimumdocumentThe inclusive lower bound of the range of shard key values.片键值范围的包含下限。
Specify each field of the shard key in the form of <fieldname> : <value>. <fieldname> : <value>的形式指定分片键的每个字段。The value must be of the same BSON type or types as the shard key. 该值必须与分片键具有相同的BSON类型。
Note
To use hashed sharding, the field value needs to be of type NumberLong. 若要使用哈希分片,字段值的类型需要为NumberLong
maximumdocumentThe exclusive upper bound of the range of shard key values.片键值范围的独占上限。
Specify each field of the shard key in the form of <fieldname> : <value>. <fieldname> : <value>的形式指定分片键的每个字段。The value must be of the same BSON type or types as the shard key. 该值必须与分片键具有相同的BSON类型。
Note
To use hashed sharding, the field value needs to be of type NumberLong. 若要使用哈希分片,字段值的类型需要为NumberLong
zonestringThe name of the zone to associate with the range of shard key values bounded by minimum and maximum.要与以minimummaximum为界的分片键值范围关联的区域的名称。

Only issue sh.updateZoneKeyRange() when connected to a mongos instance.仅在连接到mongos实例时发出sh.updateZoneKeyRange()

Behavior行为

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.例如,给定110的现有范围,则不能创建520的新范围,因为新范围将与现有范围重叠。

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.有关分片集群中区域的更多信息,请参阅区域手册页面。

Initial Chunk Distribution for Empty or Non-Existing Collections空集合或不存在集合的初始区块分布

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). 如果您正在考虑对空的或不存在的集合执行区域划分,请在划分集合之前使用sh.updateZoneKeyRange()创建区域和区域范围(自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. 从4.0.3版本开始,在空的或不存在的集合上创建区域和区域范围允许MongoDB在对集合进行分片时优化初始区块创建和分发过程。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.有关为初始区块分布定义分区和分区范围的示例,请参阅为空集合或不存在集合预先定义分区和区域范围

Initial Chunk Distribution with Compound Hashed Shard Keys具有复合散列分片键的初始块分布

Starting in version 4.4, MongoDB supports sharding collections on compound hashed indexes. 从4.4版本开始,MongoDB支持在复合哈希索引上对集合进行分片。MongoDB can perform optimized initial chunk creation and distribution when sharding the empty or non-existing collection on a compound hashed shard key.MongoDB可以在对一个复合散列的分片键进行分片时,对空的或不存在的集合进行优化的初始chunk创建和分发。

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:如果哈希字段是分片键的前缀(即分片键中的第一个字段),则MongoDB必须满足以下所有条件才能执行初始块创建和分发:

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:如果hashed字段不是分片 key的前缀(即分片 key有一个或多个非hashed前导字段),则MongoDB必须满足以下所有条件才能执行初始chunk创建和分发:

  • The collection has one zone range for each combination of distinct prefix field values (i.e. all fields preceding the hashed field).对于不同前缀字段值的每个组合(即哈希字段之前的所有字段),集合都有一个区域范围。

  • For the lower-bound of each zone range, specify MinKey for the hashed field and all subsequent fields.对于每个区域范围的下限,请为哈希字段和所有后续字段指定MinKey

  • For each zone range, at least one upper-bound prefix field must differ from its lower-bound counterpart.对于每个区域范围,必须至少有一个上界前缀字段与其下界对应字段不同。

  • sh.shardCollection() specifies the presplitHashedZones: true option.指定presplitHashedZones: true选项。

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.有关在复合哈希分片键上为初始区块分布定义区域和区域范围的更完整示例,请参阅为空集合或非现有集合预先定义区域和范围

Tip

See also: 另请参阅:

sh.balancerCollectionStatus()

Balancer平衡器

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.有关迁移如何在分片集群中工作的更多信息,请参阅分片集群平衡器手册页面。

Bounds边界

Zone ranges are always inclusive of the lower boundary and exclusive of the upper boundary.区域范围始终包括下边界,不包括上边界。

Dropped Collections丢弃的集合

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.在早期版本中,MongoDB不会删除已删除集合的标记关联,如果以后创建具有相同名称的新集合,则旧的标记关联将应用于新集合。

Security安全

For sharded clusters running with authentication, you must authenticate as either:对于使用身份验证运行的分片集群,您必须以以下方式之一进行身份验证

  • a user whose privileges include the specified actions on various collections in the config database:权限包括对config数据库中的各种集合执行指定操作的用户:

    • find on the config.shards collectionconfig.shards集合上的find
    • find and update on the config.tags collection;config.tags集合上的findupdate

    or, alternatively,或者,

  • a user whose privileges include enableSharding on the cluster resource (available starting in version 4.2.2, 4.0.14, 3.6.16).权限包括在集群资源上enableSharding的用户(从版本4.2.2、4.0.14、3.6.16开始可用)。

The clusterAdmin or clusterManager built-in roles have the appropriate permissions for issuing sh.updateZoneKeyRange(). clusterAdminclusterManager内置角色具有发出sh.updateZoneKeyRange()的适当权限。See the documentation page for Role-Based Access Control for more information.有关详细信息,请参阅基于角色的访问控制的文档页。

Examples实例

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:给定一个分片集合exampledb.collection,其分片键为{ a : 1 },以下操作在alpha区域上创建一个下限为1、上限为10的范围:

sh.updateZoneKeyRange(
"exampledb.collection",
{ a : 1 },
{ a : 10 },
"alpha"
)

The following operation removes the previously created range by passing null to the zone field.以下操作通过向zone字段传递null来删除以前创建的范围。

sh.updateZoneKeyRange(
"exampledb.collection",
{ a : 1 },
{ a : 10 },
null
)

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:minmax必须与目标范围的边界完全匹配。以下操作尝试删除以前创建的范围,但将{ a : 0 }指定为min边界:

sh.updateZoneKeyRange(
"exampledb.collection",
{ a : 0 },
{ a : 10 },
null
)

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.虽然{ a : 0 }{ a : 10 }的范围包含现有范围,但它并不完全匹配,因此updateZoneKeyRange不会删除任何内容。

Compound Shard Key复合分片键

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:给定一个分片集合exampledb.collection,其分片键为{ a : 1, b : 1 },以下操作将创建一个覆盖下限{ a: 1, b : 1 }和上限{ a : 10, b : 10}的范围,并将其与alpha区域关联:

sh.updateZoneKeyRange(
"exampledb.collection",
{ a : 1, b : 1 },
{ a : 10, b : 10 },
"alpha"
)

Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection为空集合或不存在集合预先定义分区和分区范围

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. 从4.2版本开始,在空的或不存在的集合上创建区域和区域范围允许MongoDB在对集合进行分片时优化初始区块创建和分发过程。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.有关详细信息,请参阅使用复合哈希分片键的初始块分布

The sections below contain examples for three different shard key types.下面的部分包含三种不同分片键类型的示例。

Consider the following examples, which explore pre-defining zones or zone ranges for three different shard key types:考虑以下示例,这些示例探索了三种不同分片键类型的预定义区域或区域范围:

Single or Compound Shard Keys单个或复合分片键

Note

This example only applies to single-field or compound shard keys without a hashed field.此示例仅适用于没有哈希字段的单个字段或复合分片键。

For example, { "zip" : 1 } or { "zip" : 1, "account" : 1}例如,{ "zip" : 1 }{ "zip" : 1, "account" : 1}

1
Create the Zones创建分区

Use sh.addShardToZone() to create the zones:使用sh.addShardToZone()创建分区:

sh.addShardToZone("shardA", "DC1")
sh.addShardToZone("shardB", "DC2")
2
Create the Zone Ranges创建分区范围

Use sh.updateZoneKeyRange() to create the ranges for the empty contacts collection in the exampledb database:使用sh.updateZoneKeyRange()exampledb数据库中的空contacts集合创建范围:

sh.updateZoneKeyRange(
"exampledb.contacts",
{ zip: 10001 },
{ zip: 10090 },
"DC1"
);

sh.updateZoneKeyRange(
"exampledb.contacts",
{ zip: 90001 },
{ zip: 96054 },
"DC2"
);
3
Shard the Collection粉碎集合
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:使用sh.shardCollection()对集合contacts进行分片:

sh.shardCollection("exampledb.contacts",  { zip: 1 } );
4
Review the Created Chunks and Distribution查看创建的区块和分布

To see the created chunks and distribution, run the sh.status() operation:要查看创建的区块和分布,请运行sh.status()操作:

sh.status()

The method returns:该方法返回:

--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5b80c06d35a961fd0ae1986d")
}
shards:
{ "_id" : "shardA", "host" : "shardA/mongodb0.example.net:27018,mongodb1.example.net:27018,mongodb2.example.net:27018", "state" : 1, "tags" : [ "DC1" ] }
{ "_id" : "shardB", "host" : "shardB/mongodb3.example.net:27018,mongodb4.example.net:27018,mongodb5.example.net:27018", "state" : 1, "tags" : [ "DC2" ] }
active mongoses:
"4.2.0" : 2
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
{ "_id" : "exampledb", "primary" : "shardA", "partitioned" : true, "version" : { "uuid" : UUID("6c351bcf-acd2-4fd9-82d8-9f6bd7321558"), "lastMod" : 1 } }
exampledb.contacts
shard key: { "zip" : 1 }
unique: false
balancing: true
chunks:
shardA 3
shardB 2
{ "zip" : { "$minKey" : 1 } } -->> { "zip" : 10001 } on : shardA Timestamp(1, 0)
{ "zip" : 10001 } -->> { "zip" : 10090 } on : shardA Timestamp(1, 1)
{ "zip" : 10090 } -->> { "zip" : 90001 } on : shardB Timestamp(1, 2)
{ "zip" : 90001 } -->> { "zip" : 96054 } on : shardB Timestamp(1, 3)
{ "zip" : 96054 } -->> { "zip" : { "$maxKey" : 1 } } on : shardA Timestamp(1, 4)
tag: DC1 { "zip" : 10001 } -->> { "zip" : 10090 }
tag: DC2 { "zip" : 90001 } -->> { "zip" : 96054 }

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.对于集合,分片操作在shardA和shardB之间创建了5个块(两个块对应于区域范围,另外三个块覆盖所有其他值)。

Compound Hashed Shard Key with Hashed Prefix带哈希前缀的复合哈希分片键

Note

This example 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. 从4.4版本开始,MongoDB支持在复合散列索引上对集合进行分片。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.当对复合散列的分片键进行分片时,只有在定义的区域范围满足额外要求的情况下,MongoDB才能对空的或不存在的集合执行优化的初始块创建和分发。

Consider an empty collection examples.metrics which will store analytics from one of two manufacturing facilities. 考虑一个空集合examples.metrics,它将存储来自两个制造工厂之一的分析。The planned shard key is { "_id" : "hashed", "facility" : 1}, where the hashed field is the shard key prefix.计划的分片键是{ "_id" : "hashed", "facility" : 1},其中哈然而 字段是分片键前缀

1
Create the Zones创建分区

The planned shard key is { "_id" : "hashed", "facility" : 1 }. 计划的分片键为{ "_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():由于哈然而 字段是前缀(即分片键中的第一个字段),因此使用sh.addShardToZone()创建一个区域:

sh.addShardToZone("shardA", "FacilityAlpha")
sh.addShardToZone("shardB", "FacilityAlpha")
2
Create the Zone Ranges创建分区范围

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.具有哈希前缀的复合哈希分片键上的初始区块分布需要一个单独的区域范围,所有下界字段为MinKey,所有上界字段为MaxKey

Use sh.updateZoneKeyRange() to create a single range:使用sh.updateZoneKeyRange()创建单个范围:

sh.updateZoneKeyRange(
"examples.metrics",
{ "_id" : MinKey, "facility" : MinKey },
{ "_id" : MaxKey, "facility" : MaxKey },
"FacilityAlpha"
);
3
Shard the Collection分片集合
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:使用sh.shardCollection()presplitHashedZones: true对集合进行分片,并执行初始区块创建和分发:

sh.shardCollection(
"examples.metrics",
{ "_id" : "hashed", "facility" : 1 },
false,
{ presplitHashedZones: true }
)
4
Review the Created Chunks and Distribution查看创建的区块和分布

To see the created chunks and distribution, run the sh.status() operation:要查看创建的区块和分布,请运行sh.status()操作:

sh.status()

The output resembles the following (content omitted for readability):输出类似于以下内容(为了可读性,省略了内容):

--- Sharding Status ---

databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
{ "_id" : "examples", "primary" : "shardA", "partitioned" : true, "version" : { "uuid" : UUID("245f8abf-a363-48b0-8208-2a5b577bbb4e"), "lastMod" : 1 } }
examples.metrics
shard key: { "_id" : "hashed", "facility" : 1 }
unique: false
balancing: true
chunks:
shardA 2
shardB 2
{ "_id" : { "$minKey" : 1 }, "facility" : { "$minKey" : 1 } } -->> { "_id" : NumberLong("-4611686018427387902"), "facility" : { "$minKey" : 1 } } on : shardA Timestamp(1, 0)
{ "_id" : NumberLong("-4611686018427387902"), "facility" : { "$minKey" : 1 } } -->> { "_id" : NumberLong(0), "facility" : { "$minKey" : 1 } } on : shardA Timestamp(1, 1)
{ "_id" : NumberLong(0), "facility" : { "$minKey" : 1 } } -->> { "_id" : NumberLong("4611686018427387902"), "facility" : { "$minKey" : 1 } } on : shardB Timestamp(1, 2)
{ "_id" : NumberLong("4611686018427387902"), "facility" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 }, "facility" : { "$maxKey" : 1 } } on : shardB Timestamp(1, 3)

tag: FacilityAlpha { "_id" : { "$minKey" : 1 }, "facility" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 }, "facility" : { "$maxKey" : 1 } }

The sharding operation produced 4 chunks in total. Two chunks correspond to the absolute lower and upper bounds. 分片操作总共产生了4个块。两个块对应于绝对的下限和上限。One zone was created on shardA and shardB corresponding to FacilityAlpha. 在与FacilityAlpha对应的shardAshardB上创建了一个区域。The zone was subdivided into 2 chunks using the hashed field.使用散列字段将该区域细分为2个块。

Compound Hashed Shard Key with Non-Prefix Hashed Field具有非前缀哈希字段的复合哈希分片键

Note

This example 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. 从4.4版本开始,MongoDB支持在复合哈希索引上对集合进行分片。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.当对复合散列的分片键进行分片时,只有在定义的区域范围满足额外要求的情况下,MongoDB才能对空的或不存在的集合执行优化的初始块创建和分发。

Consider an empty collection examples.metrics which will store analytics from one of two manufacturing facilities. 考虑一个空集合examples.metrics,它将存储来自两个制造工厂之一的分析。The planned shard key is { "facility" : 1, "_id" : "hashed" }, where the hashed field is not the shard key prefix.计划的分片键为{ "facility" : 1, "_id" : "hashed" },其中哈希字段不是分片键前缀。

  • The facility field stores the name of the facility: "FacilityAlpha" or "FacilityBaker". facility字段存储设施的名称:"FacilityAlpha""FacilityBaker"The collection requires zone ranges on facility to help isolate data for each facility to specific shards.该集合需要设施上的区域范围,以帮助将每个设施的数据隔离到特定的分片。
  • The _id field compensates for the low-cardinality of the facility field. _id字段补偿了facility字段的低基数。Hashing compensates for the monotonically-increasing nature of the _id field.散列补偿了_id字段的单调增加性质。
1
Create the Zones创建分区

Use the sh.addShardToZone() command to create the zones.使用sh.addShardToZone()命令创建分区。

sh.addShardToZone("shardA", "FacilityAlpha")
sh.addShardToZone("shardB", "FacilityBaker")
2
Create the Zone Ranges创建分区范围

The planned shard key is {"facility" : 1, "_id" : "hashed"}. 计划的分片键为{"facility" : 1, "_id" : "hashed"}The facility field has two possible values: FacilityAlpha and FacilityBaker.facility字段有两个可能的值:FacilityAlphaFacilityBaker

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.由于facility有两个不同的前缀值,因此集合正好需要两个覆盖这些值的区域范围。

  • The lower bound range specifies MinKey for all non-prefix fields.下限范围为所有非前缀字段指定MinKey
  • The upper-bound range has at least one prefix field that differs from its lower-bound counterpart.上限范围至少有一个前缀字段与其下限对应字段不同。

Use sh.updateZoneKeyRange() to create the range for "facility": "FacilityAlpha":使用sh.updateZoneKeyRange()"facility": "FacilityAlpha"创建范围:

sh.updateZoneKeyRange(
"examples.metrics",
{ "facility": "FacilityAlpha", "_id" : MinKey },
{ "facility": "FacilityBaker", "_id" : MinKey },
"FacilityAlpha"
);
  • Since zone range upper bounds are exclusive, this range only covers documents with the distinct shard key prefix value "facilty" : "FacilityAlpha" and all possible values of _id.由于区域范围的上限是排他性的,因此该范围仅涵盖具有不同分片键前缀值"facilty" : "FacilityAlpha"_id的所有可能值的文档。

Use sh.updateZoneKeyRange() to create the range for "facility": "FacilityBaker":使用sh.updateZoneKeyRange()"facility": "FacilityBaker"创建范围:

sh.updateZoneKeyRange(
"examples.metrics",
{ "facility": "FacilityBaker", "_id" : MinKey },
{ "facility": MaxKey, "_id" : MinKey },
"FacilityBaker"
);
  • While the upper bound of this range can technically capture other values of facility, the initial chunk distribution logic relies on the assumption that no other distinct values for facility exist. 虽然该范围的上限在技术上可以捕获facility的其他值,但初始块分布逻辑依赖于不存在facility的其他不同值的假设。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.由于集合仅包含facilityFacilityAlphaFacilityBaker的文档,因此此范围仅涵盖具有不同分片键前缀值"facility" : "FacilityBaker"和所有可能值_id的文档。
3
Shard the Collection分片集合
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:使用sh.shardCollection()presplitHashedZones: true对集合进行分片,并执行初始区块创建和分发:

sh.shardCollection(
"examples.metrics",
{ "facility" : 1, "_id" : "hashed"},
false,
{ presplitHashedZones: true }
)
4
Review the Created Chunks and Distribution查看创建的区块和分布

To see the created chunks and distribution, run the sh.status() operation:要查看创建的区块和分布,请运行sh.status()操作:

sh.status()

The output resembles the following (content omitted for readability):输出类似于以下内容(为了可读性,省略了内容):

--- Sharding Status ---

databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
{ "_id" : "examples", "primary" : "shardA", "partitioned" : true, "version" : { "uuid" : UUID("6c351bcf-acd2-4fd9-82d8-9f6bd7321558"), "lastMod" : 1 } }
examples.metrics
shard key: { "facility" : 1, "_id" : "hashed" }
unique: false
balancing: true
chunks:
shardA 3
shardB 3
{ "facility" : { "$minKey" : 1 }, "_id" : { "$minKey" : 1 } } -->> { "facility" : "FacilityAlpha", "_id" : { "$minKey" : 1 } } on : shard1 Timestamp(1, 0)
{ "facility" : "FacilityAlpha", "_id" : { "$minKey" : 1 } } -->> { "facility" : "FacilityAlpha", "_id" : NumberLong(0) } on : shard1 Timestamp(1, 1)
{ "facility" : "FacilityAlpha", "_id" : NumberLong(0) } -->> { "facility" : "FacilityBaker", "_id" : { "$minKey" : 1 } } on : shard1 Timestamp(1, 2)
{ "facility" : "FacilityBaker", "_id" : { "$minKey" : 1 } } -->> { "facility" : "FacilityBaker", "_id" : NumberLong(0) } on : shard2 Timestamp(1, 3)
{ "facility" : "FacilityBaker", "_id" : NumberLong(0) } -->> { "facility" : { "$maxKey" : 1 }, "_id" : { "$minKey" : 1 } } on : shard2 Timestamp(1, 4)
{ "facility" : { "$maxKey" : 1 }, "_id" : { "$minKey" : 1 } } -->> { "facility" : { "$maxKey" : 1 }, "_id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 5)

tag: FacilityAlpha { "facility" : "FacilityAlpha", "_id" : { "$minKey" : 1 } } -->> { "facility" : "FacilityBaker", "_id" : { "$minKey" : 1 } }
tag: FacilityBaker { "facility" : "FacilityBaker", "_id" : { "$minKey" : 1 } } -->> { "facility" : { "$maxKey" : 1 }, "_id" : { "$minKey" : 1 } }

The sharding operation produced 6 chunks in total. Two chunks correspond to the absolute lower and upper bounds. 分片操作总共产生了6个块。两个块对应于绝对的下限和上限。Two zones were created, one on shardA and one on shardB, corresponding to FacilityAlpha and FacilityBaker. 创建了两个区域,一个在shardA上,另一个在shardB上,分别对应于FacilityAlphaFacilityBakerEach of these zones has been further subdivided into 2 chunks using the hashed field.使用散列字段将这些区域中的每个区域进一步细分为2个块。

Tip

See also: 另请参阅: