sh.updateZoneKeyRange()

On this page本页内容

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()

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

Parameter参数Type类型Description描述
namespacestring

The namespace of the sharded collection associate with the zone.与区域关联的分片集合的命名空间

The collection must be sharded for the operation to succeed.必须对集合进行分片,操作才能成功。

minimumdocument

The 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类型。

maximumdocument

The 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可以执行优化的初始区块创建和分发。

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必须满足以下所有条件才能执行初始块创建和分发:

  • The collection has a single zone range with MinKey for all lower-bound fields and MaxKey for all upper-bound fields.集合有一个单一区域范围,所有下限字段使用MinKey,所有上限字段使用MaxKey

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

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

  • 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: 参阅:

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删除的集合

Starting in MongoDB 4.0.2, dropping a collection deletes its associated zone/tag ranges.从MongoDB 4.0.2开始,删除集合会删除其关联的区域/标记范围。

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:对于使用authentication运行的分片群集,必须使用以下身份验证:

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

    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.以下操作通过向区域字段传递null来删除先前创建的范围。

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

The min and max must match exactly the bounds of the target range. minmax必须精确匹配目标范围的边界。The following operation attempts to remove the previously created range, but specifies { a : 0 } as the min bound:以下操作尝试删除以前创建的范围,但将{a:0}指定为最小界限:

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
Optional: Enable Sharding for the Database可选:为数据库启用分片

Skip this step if you already enabled sharding on the database.如果已经在数据库上启用了分片,请跳过此步骤。

Use sh.enableSharding() to enable sharding for the database:使用sh.enableSharding()为数据库启用分片:

sh.enableSharding("exampledb")
4
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 } );
5
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
Optional: Enable Sharding for the Database可选:为数据库启用分片

Skip this step if you already enabled sharding on the database.如果已经在数据库上启用了分片,请跳过此步骤。

Use sh.enableSharding() to enable sharding for the database:使用sh.enableSharding()为数据库启用分片:

sh.enableSharding("examples")
4
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 }
)
5
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.集合需要facility上的区域范围,以帮助将每个设施的数据隔离到特定分片。
  • 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.设施字段有两个可能的值: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的其他值,但初始区块分布逻辑依赖于设施不存在其他不同值的假设。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
Optional: Enable Sharding for the Database可选:为数据库启用分片

Skip this step if you already enabled sharding on the database.如果已经在数据库上启用了分片,请跳过此步骤。

Use sh.enableSharding() to enable sharding for the database:使用sh.enableSharding()为数据库启用分片:

sh.enableSharding("examples")
4
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 }
)
5
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. 分片操作总共产生了6个块。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. 创建了两个区域,一个在shardA上,另一个在shardB上,对应于FacilityAlphaFacilityBakerEach of these zones has been further subdivided into 2 chunks using the hashed field.使用散列字段将每个区域进一步细分为2个块。

Tip提示
←  sh.waitForPingChange()convertShardKeyToHashed →