On this page本页内容
reshardCollection
New in version 5.0.在版本5.0中新增。
The reshardCollection
command changes the shard key for a collection and changes the distribution of your data.reshardCollection
命令更改集合的分片键,并更改数据的分布。
The command has the following syntax:该命令具有以下语法:
{ reshardCollection: "<database>.<collection>", key: <shardkey>, unique: <boolean>, numInitialChunks: <integer>, collation: { locale: "simple" }, zones: [ { min: <document with same shape as shardkey>, max: <document with same shape as shardkey>, zone: <string> | null }, ... ] }
The command takes the following fields:该命令接受以下字段:
reshardCollection | string | <database>.<collection> . <database>.<collection> 。 |
key | document |
|
unique | boolean | false is supported. false 。false . false 。 |
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 |
The mongosh
provides a wrapper method sh.reshardCollection()
.mongosh
提供了一个包装方法sh.reshardCollection()
。
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 如果需要,可以通过发出commitReshardCollection
command. 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 commitReshardCollection
command blocks writes early and forces the resharding operation to complete. commitReshardCollection
命令阻止早期写入,并强制重新加载操作完成。During the time period where writes are blocked your application experiences an increase in latency.在写入被阻止的时间段内,应用程序的延迟会增加。
abortReshardCollection
.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
集合:
db.adminCommand({ reshardCollection: "sales.orders", key: { 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) }