Docs HomeMongoDB Manual

sh.addTagRange()

On this page本页内容

Definition定义

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

Changed in version 3.43.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.4 provides Zone sharding as the successor to tag-aware sharding.下面指定的功能仍然适用于MongoDB 3.2。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可以在对一个复合散列的分片键进行分片时,对空的或不存在的集合进行优化的初始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:如果hashed字段是分片 key的前缀(即分片 key中的第一个字段),则MongoDB必须满足以下所有条件才能执行初始chunk创建和分发:

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

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

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