Docs HomeMongoDB Manual

sh.reshardCollection()

Definition定义

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()方法更改集合的分片键并更改数据的分布。

Important

mongosh 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:对于遗留的mongoshell文档,请参阅相应MongoDB Server版本的文档:

mongo shell v4.4

sh.reshardCollection() takes the following arguments:采用以下参数:

Parameter参数Type类型Description描述
namespacestringThe namespace of the collection to shard in the form "<database>.<collection>".集合的命名空间"<database>.<collection>"的形式分片。
keydocumentThe document that specifies the new field or fields to use as the shard key.指定要用作分片键的一个或多个新字段的文档。
{ <field1>: <1|"hashed">, ... }
Set the field values to either: 将字段值设置为: See also Shard Key Indexes 另请参阅分片键索引
uniquebooleanOptional.可选的。Specify whether there is a uniqueness constraint on the shard key. 指定分片关键帧上是否存在唯一性约束。Only false is supported. Defaults to false.只支持false。默认为false
optionsdocumentOptional.可选的。A document containing optional fields, including numInitialChunks, collation and zones.包含可选字段的文档,包括numInitialChunkscollationzones

The options argument supports the following options:options参数支持以下选项:

Parameter参数Type类型Description描述
numInitialChunksintegerOptional.可选的。Specifies the initial number of chunks to create across all shards in the cluster when resharding a collection. 指定在重新分片集合时要在集群中所有分片上创建的块的初始数量。The default is the number of chunks that exist for the collection under the current shard key pattern. 默认值是当前分片键模式下集合中存在的块数。MongoDB will then create and balance chunks across the cluster. The numInitialChunks must result in less than 8192 per shard.然后MongoDB将在集群中创建并平衡区块。numInitialChunks必须导致每个分片少于8192个。
collationdocumentOptional.可选的。If the collection specified to reshardCollection has a default collation, you must include a collation document with { locale : "simple" }, or the reshardCollection command fails.如果指定给reshardCollection的集合具有默认排序规则,则必须包含具有{ locale : "simple" }的排序规则文档,否则reshardCollection命令将失败。
zonesarrayOptional.可选的。To maintain or add zones, specify the zones for your collection in an array: 若要维护或添加分区,请在数组中为集合指定分区:
[
{
min: <document with same shape as shardkey>,
max: <document with same shape as shardkey>,
zone: <string> | null
},
...
]

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.当完成重新分片操作的剩余时间估计不到两秒时,重新分片协调器会阻止对集合的写入。

    Note

    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.在写入被阻止的时间段内,应用程序的延迟会增加。

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.每个捐赠分片都会丢弃旧的分片集合。

    Tip

    See also: 另请参阅:

    Reshard a Collection重新分片集合

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)
}
Tip

See also: 另请参阅:

Reshard a Collection重新分片集合