On this page本页内容
sh.reshardCollection(namespace, key, unique, options)
New in version 5.0.在版本5.0中新增。
The sh.reshardCollection()
method changes the shard key for a collection and changes the distribution of your data.sh.reshardCollection()
方法更改集合的分片键并更改数据的分布。
sh.reshardCollection()
takes the following arguments:采用以下参数:
namespace | string | "<database>.<collection>" . "<database>.<collection>" 。 |
key | document |
|
unique | boolean | false is supported. false 。false .false 。 |
options | document | numInitialChunks , collation and zones .numInitialChunks 、collation 和zones 。 |
The options
argument supports the following options:options
参数支持以下选项:
numInitialChunks | integer | numInitialChunks must result in less than 8192 per shard. numInitialChunks 必须导致每个分片小于8192 。 |
collation | document | reshardCollection has a default collation, you must include a collation document with { locale : "simple" } , or the reshardCollection command fails. reshardCollection 的集合具有默认排序规则,则必须包含带有{ locale : "simple" } 的排序规则文档,否则reshardCollection 命令将失败。 |
zones | array |
[
{
min: <document with same shape as shardkey>,
max: <document with same shape as shardkey>,
zone: <string> | null
},
...
]
|
During the resharding process, there are two roles a shard may play:在重新存储过程中,分片可能扮演两个角色:
A shard may play both the role of a donor and a recipient concurrently. 分片可以同时扮演捐赠者和接收者的角色。Unless zones are being used, the set of donor shards is the same as the set of recipient shards.除非使用区域,否则捐赠者分片集与接收者分片集相同。
The config server primary is always chosen as the resharding coordinator, responsible for initiating each phase of the process.配置服务器主服务器总是被选为重新分配协调器,负责启动流程的每个阶段。
During the initialization phase:在初始化阶段:
During the index phase:在索引阶段:
During the clone, apply, and catch-up phase:在克隆、应用和追赶阶段:
When the estimate for the time remaining to complete the resharding operation is under two seconds, the resharding coordinator blocks writes for the collection.当完成重新装载操作的剩余时间估计在两秒以下时,重新装载协调器将阻止对集合的写入。
If desired, you can manually force the resharding operation to complete by issuing the 如果需要,可以通过发出sh.commitReshardCollection()
method. sh.commitReshardCollection()
方法手动强制完成重新存储操作。This is useful if the current time estimate to complete the resharding operation is an acceptable duration for your collection to block writes. 如果完成重新存储操作的当前估计时间是集合阻止写入的可接受持续时间,则此选项非常有用。The sh.commitReshardCollection()
method blocks writes early and forces the resharding operation to complete. sh.commitReshardCollection()
方法会阻止提前写入,并强制完成重新存储操作。During the time period where writes are blocked your application experiences an increase in latency.在写操作被阻止的时间段内,应用程序的延迟会增加。
sh.abortReshardCollection()
.sh.abortReshardCollection()
中止它。Each donor shard drops the old sharded collection.每个捐赠分片都会丢弃旧的分片集合。
The following example reshards the 以下示例使用新的分片键sales.orders
collection with the new shard key { order_id: 1 }
:{ order_id: 1 }
重新划分sales.orders
集合:
sh.reshardCollection("sales.orders", { order_id: 1 })
MongoDB returns the following:MongoDB返回以下内容:
{ ok: 1, '$clusterTime': { clusterTime: Timestamp(1, 1624887954), signature: { hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), keyId: 0 } }, operationTime: Timestamp(1, 1624887947) }