sh.addTagRange()

On this page本页内容

Definition定义

sh.addTagRange(namespace, minimum, maximum, tag)

Changed in version 3.4.在版本3.4中更改

This method aliases to sh.updateZoneKeyRange() in MongoDB 3.4. 此方法在MongoDB 3.4中别名sh.updateZoneKeyRange()The functionality specified below still applies to MongoDB 3.2. 下面指定的功能仍然适用于MongoDB 3.2。MongoDB 3.4 provides Zone sharding as the successor to tag-aware sharding.MongoDB 3.4提供分区分片作为标记感知分片的后续版本。

Attaches a range of shard key values to a shard tag created using the sh.addShardTag() method.将一系列分片键值附加到使用sh.addShardTag()方法创建的分片标记。

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.addTagRange() takes the following arguments:采用以下参数:

Parameter参数Type类型Description描述
namespacestringThe namespace of the sharded collection to tag.要标记的分片集合的命名空间
minimumdocumentThe minimum value of the shard key range to include in the tag. 要包含在标记中的分片键范围的最小值。The minimum is an inclusive match. 最小值是包含匹配项。Specify the minimum value in the form of <fieldname>:<value>. <fieldname>:<value>的形式指定最小值。This value must be of the same BSON type or types as the shard key. 该值必须与分片键具有相同的BSON类型。
maximumdocumentThe maximum value of the shard key range to include in the tag. 要包含在标记中的分片键范围的最大值。The maximum is an exclusive match. 最大值为独占匹配。Specify the maximum value in the form of <fieldname>:<value>. <fieldname>:<value>的形式指定最大值。This value must be of the same BSON type or types as the shard key. 该值必须与分片键具有相同的BSON类型。
tagstringThe name of the tag to attach the range specified by the minimum and maximum arguments to. 要附加由minimummaximum参数指定的范围的标记的名称。

Use sh.addShardTag() to ensure that the balancer migrates documents that exist within the specified range to a specific shard or set of shards.使用sh.addShardTag()确保平衡器将存在于指定范围内的文档迁移到特定分片或分片集。

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

Behavior行为

Bounds边界

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

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.addTagRange() to create the zones and zone ranges before sharding the collection. 如果您正在考虑对空集合或不存在的集合执行区域分片,请在分片集合之前使用sh.addTagRange()创建区域和区域范围。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必须满足以下所有条件才能执行初始块创建和分发:

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:如果哈希字段不是分片键的前缀(即分片键有一个或多个非哈希前导字段),那么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.有关为复合散列分片键上的初始区块分发定义区域和区域范围的更完整示例,请参阅为空集合或不存在集合预定义区域和区域区域范围

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不会删除已删除集合的标记关联,如果稍后创建同名的新集合,则旧的标记关联将应用于新集合。

Example示例

Given a shard key of {state: 1, zip: 1}, the following operation creates a tag range covering zip codes in New York State:给定{state: 1, zip: 1}的分片键,以下操作将创建一个包含纽约州邮政编码的标记范围:

sh.addTagRange( "exampledb.collection",
                { state: "NY", zip: MinKey },
                { state: "NY", zip: MaxKey },
                "NY"
              )
←  sh.addShardToZone()sh.balancerCollectionStatus() →