sh.reshardCollection()
On this page本页内容
Definition定义
sh.reshardCollection(namespace, key, unique, options)
New in version 5.0.5.0版新增。Thesh.reshardCollection()
method changes the shard key for a collection and changes the distribution of your data.sh.reshardCollection()
方法更改集合的分片键并更改数据的分布。Importantmongosh Method
This page documents a本页记录了一个mongosh
method.mongosh
方法。This is not the documentation for database commands or language-specific drivers, such as Node.js.这不是数据库命令或特定语言驱动程序(如Node.js)的文档。For the database command, see the有关数据库命令,请参阅reshardCollection
command.reshardCollection
命令。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。For the legacy对于遗留的mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:mongo
shell文档,请参阅相应MongoDB Server版本的文档:sh.reshardCollection()
takes the following arguments:采用以下参数:Parameter参数Type类型Description描述namespace
string The namespace of the collection to shard in the form集合的命名空间以"<database>.<collection>"
."<database>.<collection>"
的形式分片。key
document The document that specifies the new field or fields to use as the shard key.指定要用作分片键的一个或多个新字段的文档。
{ <field1>: <1|"hashed">, ... }
Set the field values to either:将字段值设置为:1
for ranged based sharding用于基于范围的分片"hashed"
to specify a hashed shard key.以指定哈希分片键。
See also Shard Key Indexes另请参阅分片键索引unique
boolean Optional.可选的。Specify whether there is a uniqueness constraint on the shard key.指定分片关键帧上是否存在唯一性约束。Only只支持false
is supported. Defaults tofalse
.false
。默认为false
。options
document Optional.可选的。A document containing optional fields, including包含可选字段的文档,包括numInitialChunks
,collation
andzones
.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 | [ |
Resharding Process重新分片过程
In a collection resharding operation, a shard can be a:在集合重新分片操作中,分片可以是:
donor, which currently stores chunks for the sharded collection.捐献者,它当前为分片集合存储块。recipient, which stores new chunks for the sharded collection based on the shard keys and zones.接受者,它根据分片键和区域为分片集合存储新的块。
A shard can be donor and a recipient at the same time. The set of donor shards is identical to the recipient shards, unless you use zones.一个分片可以同时是捐赠者和接受者。供体分片集与受体分片集相同,除非使用区域。
The config server primary is always the resharding coordinator and starts each phase of the resharding operation.配置服务器主服务器始终是重新分片协调器,并启动重新分片操作的每个阶段。
Initialization Phase初始化阶段
During the initialization phase, the resharding coordinator determines the new data distribution for the sharded collection.在初始化阶段,重新分片协调器确定分片集合的新数据分布。
Index Phase索引阶段
During the index phase:在索引阶段:
Each shard recipient creates a new, empty sharded collection with the same collection options as the existing sharded collection.每个分片接收者都会创建一个新的空分片集合,该集合具有与现有分片集合相同的集合选项。This new sharded collection is the target for where recipient shards write the new data.这个新的分片集合是接收方分片写入新数据的目标。Each shard recipient builds the necessary new indexes.每个分片接收者都构建必要的新索引。These include all existing indexes on the sharded collection and an index compatible with the new shard key pattern if such an index doesn’t already exist on the sharded collection.其中包括分片集合上的所有现有索引,以及与新的分片键模式兼容的索引(如果分片集合中还不存在这样的索引)。
Clone, Apply, and Catch-up Phase克隆、应用和追赶阶段
During the clone, apply, and catch-up phase:在克隆、应用和追赶阶段:
Each shard recipient clones an initial copy of the documents it would own under the new shard key.每个分片接收者都会在新的分片键下克隆其将拥有的文档的初始副本。Each shard recipient begins applying oplog entries from operations that happened after the recipient cloned the data.每个分片接收方都开始应用接收方克隆数据后发生的操作中的oplog条目。When the estimate for the time remaining to complete the resharding operation is under two seconds, the resharding coordinator blocks writes for the collection.当完成重新分片操作的剩余时间估计不到两秒时,重新分片协调器会阻止对集合的写入。NoteIf 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.如果完成重新分片操作的当前时间估计值是集合阻止写入的可接受持续时间,则此选项非常有用。Thesh.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.在写入被阻止的时间段内,应用程序的延迟会增加。
Commit Phase提交阶段
Once the resharding process reaches the commit phase, it may no longer be aborted with一旦重新分片过程到达提交阶段,它可能不再使用sh.abortReshardCollection()
.sh.abortReshardCollection()
中止。When all shards have reached strict consistency, the resharding coordinator commits the resharding operation and installs the new routing table.当所有分片都达到严格的一致性时,重新分片协调器提交重新分片操作并安装新的路由表。The resharding coordinator instructs each donor and recipient shard primary, independently, to rename the temporary sharded collection. The temporary collection becomes the new resharded collection.重新分片协调器分别指示每个施主和受主分片器重命名临时分片集合。临时集合将成为新的重新分片的集合。Each donor shard drops the old sharded collection.每个捐赠分片都会丢弃旧的分片集合。TipSee also:另请参阅:
Example实例
Reshard a 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)
}