On this page本页内容
In sharded clusters, you can create zones of sharded data based on the shard key. 在分片集群中,可以基于分片密钥创建分片数据区域。You can associate each zone with one or more shards in the cluster. 可以将每个分区与群集中的一个或多个分片相关联。A shard can associate with any number of zones. 分片可以与任意数量的区域关联。In a balanced cluster, MongoDB migrates chunks covered by a zone only to those shards associated with the zone.在平衡集群中,MongoDB只将区域覆盖的块迁移到与该区域相关联的分片。
Some common deployment patterns where zones can be applied are as follows:可以应用区域的一些常见部署模式如下:
The following image illustrates a sharded cluster with three shards and two zones. 下图显示了一个包含三个分片和两个区域的分片簇。The A区域表示下限为A
zone represents a range with a lower boundary of 1
and an upper bound of 10
. 1
、上限为10
的范围。The B
zone represents a range with a lower boundary of 10
and an upper boundary of 20
. B
区代表一个下限为10
、上限为20
的范围。Shards 分片Alpha
and Beta
have the A
zone. Alpha
和Beta
具有A
区域。Shard 分片Beta
also has the B
zone. Beta
也有B区。Shard 分片Charlie
has no zones associated with it. Charlie
没有与之相关的区域。The cluster is in a steady state and no chunks violate any of the zones.集群处于稳定状态,没有块体违反任何区域。
Each zone covers one or more ranges of shard key values for a collection. 每个分区覆盖一个集合的一个或多个分片键值范围。Each range a zone covers is always inclusive of its lower boundary and exclusive of its upper boundary. 区域覆盖的每个范围始终包括其下边界,不包括其上边界。Zones cannot share ranges, nor can they have overlapping ranges.分区不能共享范围,也不能有重叠的范围。
For example, consider a shard key on 例如,考虑{"x": 1}
. {"x": 1}
上的分片键。The cluster has the following zone ranges:集群具有以下区域范围:
{ "x" : 5 } --> { "x" : 10 } // Zone A { "x" : 10} --> { "x" : 20 } // Zone B
7
is routed to zone A.7
的文档被路由到区域A。10
is routed to zone B.10
的文档被路由到区域B。For collections whose shard key includes a hashed field, zone ranges and data distribution on that field are on hashed values. 对于分片键包含散列字段的集合,该字段上的区域范围和数据分布基于散列值。The zone contains documents whose hashed shard key value falls into the defined range. 该区域包含哈希分片键值在定义范围内的文档。A zone range on a hashed field does not have the same predictable document routing behavior as a zone range on an unhashed field.散列字段上的区域范围与未散列字段上的区域范围不具有相同的可预测文档路由行为。
For example, consider a shard key on 例如,考虑{"x" : "hashed"}
. {"x" : "hashed"}
上的分片键。The following range represents the hashed range between 以下范围表示5
and 10
:5
到10
之间的散列范围:
{ "x": NumberLong("4470791281878691347") } --> { "x": NumberLong("7766103514953448109") } // Zone A
1
is routed to Zone A since the hashed value of 1
falls into the defined range.15
is routed to Zone A since the hashed value of 15
falls into the defined range.15
的文档被路由到区域A,因为散列值为15
属于定义的范围。8
is not routed to Zone A since the hashed value of 8
does not fall into the defined range.8
的文档不会被路由到区域A,因为散列值为8
不属于定义的范围。mongosh
provides the 提供convertShardKeyToHashed()
for computing the post-hash value of the specified parameter.convertShardKeyToHashed()
,用于计算指定参数的后哈希值。
One valid use of zone ranges on a hashed field is to restrict the data for a collection to the shard or shards in a single zone. 哈希字段上区域范围的一个有效用法是将集合的数据限制为单个区域中的一个或多个分片。Create a zone range that covers the entire range of possible hashed shard key values using minKey as the lower bound and maxkey as the upper bound.使用minKey作为下限,maxkey作为上限,创建一个区域范围,覆盖可能的散列分片键值的整个范围。
To define ranges, MongoDB provides the 为了定义范围,MongoDB提供了updateZoneKeyRange
command and the associated helper methods sh.updateZoneKeyRange()
and sh.addShardTag()
.updateZoneKeyRange
命令和相关的助手方法sh.updateZoneKeyRange()
和sh.updateZoneKeyRange()
。
Starting in MongoDB 4.0.2, you can run 从MongoDB 4.0.2开始,您可以在未共享的集合或不存在的集合上运行updateZoneKeyRange
database command and its helpers sh.updateZoneKeyRange()
and sh.addTagRange()
on an unsharded collection or a non-existing collection.updateZoneKeyRange
数据库命令及其助手sh.updateZoneKeyRange()
和sh.addTagRange()
。
Starting in MongoDB 4.0.2, dropping a collection deletes its associated zone/tag ranges.从MongoDB 4.0.2开始,删除集合将删除其关联的区域/标记范围。
Changed in version 4.0.3.在版本4.0.3中更改。
Starting in version 4.4, MongoDB supports sharding collections on compound hashed indexes. 从4.4版开始,MongoDB支持在复合散列索引上分片集合。When sharding an empty or non-existing collection using a compound hashed shard key, additional requirements apply in order for MongoDB to perform initial chunk creation and distribution.当使用复合散列分片键分片空的或不存在的集合时,MongoDB执行初始块创建和分发需要满足额外的要求。
See Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection for for an example.有关示例,请参阅空集合或不存在集合的预定义分区和分区范围。
The balancer attempts to evenly distribute a sharded collection's chunks across all shards in the cluster.均衡器尝试在集群中的所有分片上均匀分布分片集合的块。
For each chunk marked for migration, the balancer checks each possible destination shard for any configured zones. 对于标记为迁移的每个块,平衡器检查每个可能的目标分片是否有任何配置的区域。If the chunk range falls into a zone, the balancer migrates the chunk into a shard inside that zone. 如果块范围落入某个区域,则平衡器将块迁移到该区域内的分片中。Chunks that do not fall into a zone can exist on any shard in the cluster and are migrated normally.不属于区域的块可以存在于集群中的任何分片上,并正常迁移。
During balancing rounds, if the balancer detects that any chunks violate the configured zones for a given shard, the balancer migrates those chunks to a shard where no conflict exists.在平衡循环期间,如果平衡器检测到任何块违反给定分片的配置区域,则平衡器将这些块迁移到不存在冲突的分片。
After associating a zone with a shard or shards and configuring the zone with a shard key range for a sharded collection, the cluster may take some time to migrate the affected data for the sharded collection. 将分区与一个或多个分片关联并为分区集合配置分区密钥范围后,集群可能需要一些时间来迁移分区集合的受影响数据。This depends on the division of chunks and the current distribution of data in the cluster. 这取决于数据块的划分和集群中数据的当前分布。When balancing is complete, reads and writes for documents in a given zone are routed only to the shard or shards inside that zone.平衡完成后,对给定区域中文档的读取和写入只路由到该区域内的一个或多个分片。
Once configured, the balancer respects zones during future balancing rounds.一旦配置完毕,均衡器将在未来的平衡回合中尊重区域。
You must use fields contained in the shard key when defining a new range for a zone to cover. 定义分区要覆盖的新范围时,必须使用分片键中包含的字段。If using a compound shard key, the range must include the prefix of the shard key.如果使用复合分片键,则范围必须包括分片键的前缀。
For example, given a shard key 例如,给定一个分片键{ a : 1, b : 1, c : 1 }
, creating or updating a range to cover values of b
requires including a
as the prefix. { a : 1, b : 1, c : 1 }
,创建或更新覆盖b
值的范围需要包含a
作为前缀。Creating or updating a range to covers values of 创建或更新覆盖c
requires including a
and b
as the prefix.c
值的范围需要将a
和b
作为前缀。
You cannot create ranges using fields not included in the shard key. 不能使用未包含在分片键中的字段创建范围。For example, if you wanted to use zones to partition data based on geographic location, the shard key would need the first field to contain geographic data.例如,如果希望使用分区根据地理位置对数据进行分区,则shard key需要第一个字段来包含地理数据。
When choosing a shard key for a collection, consider what fields you might want to use for configuring zones. 为集合选择分片键时,请考虑您可能希望使用哪些字段来配置区域。See Choose a Shard Key for considerations in choosing a shard key.有关选择分片键时的注意事项,请参阅选择分片键。
Zone ranges are always inclusive of the lower boundary and exclusive of the upper boundary.分区范围始终包括下边界,不包括上边界。
MongoDB does not support creating zones for sharded time series collections.MongoDB不支持为分片时间序列集合创建区域。