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.
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. 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.
Note
To use the refineCollectionShardKey command, the sharded cluster must have feature compatibility version (fcv) of 4.4.
The refineCollectionShardKey command must be run against the admin database and has the following form:
The refineCollectionShardKey command has the following fields:
| refineCollectionShardKey | string | The namespace of the sharded collection in the form |
| key | document | The document that specifies the field or fields to use as the new shard key for the collection.
Important
For the suffix fields, set the field values to either:
|
See also参阅
When running with access control, the user must have the refineCollectionShardKey privilege actions on database and/or collection to run the command. That is, a user must have a role that grants the following privilege:
The built-in clusterManager role provides the appropriate privileges.
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, y: 1 }{ x: 1, y: 1, a: 1, b: 1}Note
simple collation, the supporting index must specify { locale: "simple" } collation.If the current shard index has a uniqueness constraint, the new shard key index must also have a unique constraint.
refineCollectionShardKey."hashed" for any of its fields.If the sharded collection has a non-simple default 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.
To set up the example in the test database:
shardCollection operation to shard the orders collection in the test database. The operation uses the customer_id field as the initial shard key:
To modify the shard key to be the customer_id field and the order_id field { customer_id: 1, order_id: 1 },
Create the index to support the new shard key if the index does not already exist.
refineCollectionShardKey command to add the order_id field as a suffix:
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().
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.
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 Collation¶To set up the example in the test database:
cafés collection in the test database, specifying French fr as the default collation.
customer_id field as the initial shard key. Because the collection has a default fr collation and not a simple collation, the shardCollection command must include a collation:
{ locale: "simple" } option:
To modify the shard key to be both the customer_id field and the order_id field { 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.
refineCollectionShardKey command to add the order_id field as a suffix:
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().
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.
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.
See also参阅