Definition定义
refineCollectionShardKeyModifies 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.精炼集合的分片键可以解决现有键由于基数不足而导致巨型(即不可分割)块的情况。
Note
Data Distribution数据分布
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.也就是说,分片键的细化不会立即影响块在分片或区域之间的分布。任何未来的块分割或迁移都是常规分片操作的一部分。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Important
This command is not supported in M10+ clusters. For more information, see Unsupported Commands.M10+集群不支持此命令。有关详细信息,请参阅不支持的命令。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
Note
To use the 要使用refineCollectionShardKey command, the sharded cluster must have feature compatibility version (fcv) of 4.4.refineCollectionShardKey命令,分片集群必须具有4.4的功能兼容性版本(fcv)。
The command has the following syntax:该命令具有以下语法:
db.adminCommand(
{
refineCollectionShardKey: "<database>.<collection>",
key: { <existing key specification>, <suffix1>: <1|"hashed">, ... }
}
)Command Fields命令字段
The command takes the following fields:该命令包含以下字段:
refineCollectionShardKey |
| |
key |
|
Tip
Access Control访问控制
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角色提供适当的权限。
Considerations注意事项
Index Considerations指数注意事项
-
Index Existence索引存在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}
Note
The supporting index cannot be a partial index.支持索引不能是部分索引。The supporting index cannot be a sparse index.支持索引不能是稀疏索引。If the collection uses a non-如果集合使用非简单排序规则,则支持索引必须指定simplecollation, the supporting index must specify{ locale: "simple" }collation.{ locale: "simple" }排序规则。
-
Unique Index唯一索引If the current shard index has a uniqueness constraint, the new shard key index must also have a unique constraint.如果当前分片索引具有唯一性约束,则新的分片键索引也必须具有唯一性限制。After creating the unique index to support the new shard key, drop the old shard key index before running创建支持新分片键的唯一索引后,在运行refineCollectionShardKey.refineCollectionShardKey之前删除旧分片键索引。Also, if the current shard index has a unique constraint, then the new shard key cannot specify此外,如果当前分片索引具有唯一约束,则新的分片键不能为其任何字段指定"hashed"for any of its fields."hashed"。See also Sharded Collection and Unique Indexes.另请参见分片集合和唯一索引。
-
Index Collation索引排序If the sharded collection has a non-如果分片集合具有非简单的默认排序规则,则索引必须包含一个具有{locale:“simple”}的排序规则文档。simpledefault collation, then the index must include a collation document with{ locale : "simple" }.At least one of the indexes whose fields support the shard key pattern must have the simple collation.字段支持分片键模式的索引中,至少有一个必须具有简单的排序规则。
Warning
Do not modify the range or hashed type for any of the current shard key fields. It causes data inconsistencies. For example, do not modify a shard key from 不要修改任何当前分片键字段的范围或哈希类型。这会导致数据不一致。例如,不要将分片键从{ customer_id: 1 } to { customer_id: "hashed", order_id: 1 }.{ customer_id: 1 }修改为{ customer_id: "hashed", order_id: 1 }。
Examples示例
To set up the example in the 要在test database:test数据库中设置示例,请执行以下操作:
Use following使用以下shardCollectionoperation to shard theorderscollection in thetestdatabase.shardCollection操作对test数据库中的orders集合进行分片。The operation uses the该操作使用customer_idfield 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运行refineCollectionShardKeycommand to add theorder_idfield 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 }. To verify, you can run sh.status().{ customer_id: 1, order_id: 1 }。要验证,可以运行sh.status()。
Tip
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.在细化分片键之前,如果可能的话,确保集合中的所有或大多数文档都有后缀字段,以避免以后填充字段。
Collection with non-simple Collation非简单排序集合
simple CollationTo set up the example in the 要在test database:test数据库中设置示例,请执行以下操作:
Create the在caféscollection in thetestdatabase, specifying Frenchfras the default collation.test数据库中创建cafés集合,将法语fr指定为默认排序规则。db.getSiblingDB("test").createCollection( "cafés", { collation: { locale: "fr" } } );Shard the collection using使用customer_idfield as the initial shard key.customer_id字段作为初始分片键对集合进行分片。Because the collection has a default因为集合具有默认的frcollation and not asimplecollation, theshardCollectioncommand must include acollation: { locale: "simple" }option:fr排序规则而不是简单的排序规则,所以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 indexto support the new shard key if the index does not already exist. Because the collection uses a non-simple collation, the index must include thecollation: { locale: "simple" }option.collation: { locale: "simple" }选项。db.getSiblingDB("test").cafés.createIndex(
{ customer_id: 1, order_id: 1 },
{ collation: { locale: "simple" } }
)Run运行refineCollectionShardKeycommand to add theorder_idfield 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 }. To verify, you can run sh.status().customer_id:1,order_id:1}。要验证,可以运行shstatus()。
Tip
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.在细化分片键之前,如果可能的话,确保集合中的所有或大多数文档都有后缀字段,以避免以后填充字段。