Refining a collection's shard key allows for a more fine-grained data distribution and can address situations where the existing key has led to jumbo chunks due to insufficient cardinality.细化集合的分片键可以实现更细粒度的数据分布,并可以解决现有键由于基数不足而导致j巨型块的情况。
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 }。
Note
Starting in MongoDB 5.0, you can also reshard your collection by providing a new shard key for the collection. 从MongoDB 5.0开始,您还可以通过为集合提供新的分片键来重新分片集合。To learn whether you should reshard your collection or refine your shard key, see Change a Shard Key.要了解是否应该重新标记集合或优化分片键,请参阅更改分片键。
To refine a collection's shard key, use the 要优化集合的分片键,请使用refineCollectionShardKey command. refineCollectionShardKey命令。The refineCollectionShardKey adds a suffix field or fields to the existing key to create the new shard key.refineCollectionShardKey向现有键添加一个或多个后缀字段,以创建新的分片键。
For example, you may have an existing 例如,您可能在orders collection in a test database with the shard key { customer_id: 1 }. test数据库中有一个现有的orders集合,其分片键为{ customer_id: 1 }。You can use the 您可以使用refineCollectionShardKey command to change the shard key to the new shard key { customer_id: 1, order_id: 1 }:refineCollectionShardKey命令将分片键更改为新的分片键{ customer_id: 1, order_id: 1 }:
db.adminCommand( {
refineCollectionShardKey: "test.orders",
key: { customer_id: 1, order_id: 1 }
} )