New in version 4.4.在版本4.4中新增。
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.优化集合的分片键可以实现更细粒度的数据分发,并且可以解决现有键由于基数不足而导致大块的情况。
Starting in MongoDB 5.0, you can also reshard your collection by providing a new shard key for the collection.从MongoDB 5.0开始,您还可以通过为集合提供新的分片键来重分片集合。
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 } } )