On this page本页内容
refineCollectionShardKey
New in version 4.4.在版本4.4中新增。
Modifies the collection's shard key by adding new field(s) as a suffix to the existing key. 通过向现有键添加新字段作为后缀,修改集合的分片键。Refining a collection's shard key can address situations where the existing key has led to jumbo (i.e. indivisible) chunks due to insufficient cardinality.优化集合的分片键可以解决现有密钥由于基数不足而导致巨大(即不可分割)块的情况。
As part of refining the shard key, the 作为细化分片键的一部分,refineCollectionShardKey
command updates the chunk ranges and zone ranges to incorporate the new fields without modifying the range values of the existing key fields. refineCollectionShardKey
命令更新块范围和区域范围,以合并新字段,而不修改现有键字段的范围值。That is, the refinement of the shard key does not immediately affect the distribution of chunks across shards or zones. 也就是说,分片键的细化不会立即影响分片或区域之间的块分布。Any future chunk splits or migration occur as part of the routine sharding operations.任何未来的块分割或迁移都会作为常规分片操作的一部分发生。
To use the 要使用refineCollectionShardKey
command, the sharded cluster must have feature compatibility version (fcv) of 4.4
.refineCollectionShardKey
命令,分片集群的功能兼容性版本(fcv)必须为4.4
。
The refineCollectionShardKey
command must be run against the admin
database and has the following form:refineCollectionShardKey
命令必须针对admin
数据库运行,格式如下:
db.adminCommand( { refineCollectionShardKey: "<database>.<collection>", key: { <existing key specification>, <suffix1>: <1|"hashed">, ... } } )
The refineCollectionShardKey
command has the following fields:refineCollectionShardKey
命令具有以下字段:
refineCollectionShardKey | string |
|
key | document |
|
When running with access control, the user must have the 当使用访问控制运行时,用户必须对数据库和/或集合具有refineCollectionShardKey
privilege actions on database and/or collection to run the command. refineCollectionShardKey
权限操作才能运行该命令。That is, a user must have a role that grants the following privilege:即,用户必须具有授予以下权限的角色:
{ resource: { db: <database>, collection: <collection> }, actions: [ "refineCollectionShardKey" ] }
The built-in 内置的clusterManager
role provides the appropriate privileges.clusterManager
角色提供了适当的权限。
An index that supports the command's specified key must exist prior to running the command.在运行命令之前,必须存在支持命令指定键的索引。
A supporting index is an index that starts with the new shard key specification; i.e. the index prefix matches the new shard key specification. 支持索引是以新的分片键规范开头的索引;即,索引前缀匹配新的分片键规范。That is, to change the shard key to 也就是说,要将分片键从{ x: 1, y: 1 }
from { x: 1 }
, and index that starts with { x: 1, y: 1 }
must exist; e.g.{ x: 1 }
更改为{ x: 1, y: 1 }
,必须存在以{ x: 1, y: 1 }
开头的索引;如:
{ x: 1, y: 1 }
{ x: 1, y: 1, a: 1, b: 1}
simple
collation, the supporting index must specify { locale: "simple" }
collation.simple
排序规则,则支持索引必须指定{ locale: "simple" }
排序规则。If the current shard index has a uniqueness constraint, the new shard key index must also have a unique constraint.如果当前分片索引具有唯一性约束,则新的分片键索引也必须具有唯一性限制。
refineCollectionShardKey
.refineCollectionShardKey
之前删除旧的分片键索引。"hashed"
for any of its fields."hashed"
。simple
default collation, then the index must include a collation document with { locale : "simple" }
. simple
的默认排序规则,则索引必须包含带有{ locale : "simple" }
的排序规则文档。To set up the example in the 要在test
database:test
数据库中设置示例,请执行以下操作:
Enable sharding on the database, if sharding is not already enabled:如果尚未启用分片,请在数据库上启用分片:
sh.enableSharding("test")
Use following 使用以下shardCollection
operation to shard the orders
collection in the test
database. shardCollection
操作来分片test
数据库中的orders
集合。The operation uses the 该操作使用customer_id
field as the initial shard key:customer_id
字段作为初始分片键:
db.adminCommand( { shardCollection: "test.orders", key: { customer_id: 1 } } )
To modify the shard key to be the 要将分片键修改为customer_id
field and the order_id
field { customer_id: 1, order_id: 1 }
,customer_id
字段和order_id
字段{ customer_id: 1, order_id: 1 }
,
如果索引不存在,则创建索引以支持新的分片键。Create the index
to support the new shard key if the index does not already exist.
db.getSiblingDB("test").orders.createIndex( { customer_id: 1, order_id: 1 } )
Run 运行refineCollectionShardKey
command to add the order_id
field as a suffix:refineCollectionShardKey
命令以添加order_id
字段作为后缀:
db.adminCommand( { refineCollectionShardKey: "test.orders", key: { customer_id: 1, order_id: 1 } } )
Upon successful completion of the command, the shard key for the collection has changed to 成功完成命令后,集合的分片键已更改为{ customer_id: 1, order_id: 1 }
. { customer_id: 1, order_id: 1 }
。To verify, you can run 要进行验证,可以运行sh.status()
.sh.status()
。
After you refine the shard key, it may be that not all documents in the collection have the suffix field(s). 细化分片键后,可能不是集合中的所有文档都有后缀字段。To populate the missing shard key field(s), see Missing Shard Key Fields.要填充缺少的分片键字段,请参阅缺少分片键字段。
Before refining the shard key, ensure that all or most documents in the collection have the suffix fields, if possible, to avoid having to populate the field afterwards.在细化分片键之前,如果可能的话,请确保集合中的所有或大多数文档都具有后缀字段,以避免以后必须填充字段。
simple
Collationsimple
排序规则的集合To set up the example in the 要在test
database:test
数据库中设置示例,请执行以下操作:
Enable sharding on the database, if sharding is not already enabled:如果尚未启用分片,请在数据库上启用分片:
sh.enableSharding("test")
Create the 在cafés
collection in the test
database, specifying French fr
as the default collation.test
数据库中创建cafés
集合,指定French fr
作为默认排序规则。
db.getSiblingDB("test").createCollection( "cafés", { collation: { locale: "fr" } } );
Shard the collection using 使用customer_id
field as the initial shard key. customer_id
字段作为初始分片键,对集合进行分片。Because the collection has a default 由于集合具有默认的fr
collation and not a simple
collation, the shardCollection
command must include a collation: { locale: "simple" }
option:fr
排序规则,而不是simple
排序规则,因此shardCollection
命令必须包含collation: { locale: "simple" }
选项:
db.adminCommand( { shardCollection: "test.cafés", key: { customer_id: 1 }, collation: { locale: "simple" } } )
To modify the shard key to be both the 要将分片键修改为customer_id
field and the order_id
field { customer_id: 1, order_id: 1 }
,customer_id
字段和order_id
字段{ customer_id: 1, order_id: 1 }
,
如果索引不存在,则创建索引以支持新的分片键。Create the index
to support the new shard key if the index does not already exist. Because the collection uses a non-simple collation, the index must include the 因为集合使用非简单排序规则,所以索引必须包含collation: { locale: "simple" }
option.collation: { locale: "simple" }
选项。
db.getSiblingDB("test").cafés.createIndex( { customer_id: 1, order_id: 1 }, { collation: { locale: "simple" } } )
Run 运行refineCollectionShardKey
command to add the order_id
field as a suffix:refineCollectionShardKey
命令以添加order_id
字段作为后缀:
db.adminCommand( { refineCollectionShardKey: "test.cafés", key: { customer_id: 1, order_id: 1 } } )
Upon successful completion of the command, the shard key for the collection has changed to 成功完成命令后,集合的分片键已更改为{ customer_id: 1, order_id: 1 }
. { customer_id: 1, order_id: 1 }
。To verify, you can run 要进行验证,可以运行sh.status()
.sh.status()
。
After you refine the shard key, it may be that not all documents in the collection have the suffix field(s). 细化分片键后,可能不是集合中的所有文档都有后缀字段。To populate the missing shard key field(s), see Missing Shard Key Fields.要填充缺少的分片键字段,请参阅缺少分片键字段。
Before refining the shard key, ensure that all or most documents in the collection have the suffix fields, if possible, to avoid having to populate the field afterwards.在细化分片键之前,如果可能的话,请确保集合中的所有或大多数文档都具有后缀字段,以避免以后必须填充字段。