Database Manual / Reference / Database Commands / Sharding

reshardCollection (database command数据库命令)

Definition定义

reshardCollection

The reshardCollection command changes the shard key for a collection and changes the distribution of your data.reshardCollection命令更改集合的分片键并更改数据的分布。

Tip

In mongosh, this command can also be run through the sh.reshardCollection() helper method.mongosh中,此命令也可以通过sh.reshardCollection()辅助方法运行。

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。如果不需要便利性或需要额外的返回字段,请使用database命令。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.adminCommand(
{
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
},
],
forceRedistribution: <bool>
}
)

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Type类型Description描述
reshardCollectionstring字符串The namespace of the collection to be resharded. Takes the form <database>.<collection>.要重新保管的集合的命名空间。采用<database>.<collection>的形式。
keydocument文档

The document that specifies the new field or fields to use as the shard key.指定用作分片键的一个或多个新字段的文档。

{ <field1>: <1|"hashed">, ... }

Set the field values to either:将字段值设置为:

uniqueboolean布尔值Optional. 可选。Specify whether there is a uniqueness constraint on the shard key. Only false is supported. Defaults to false.指定分片键上是否存在唯一性约束。只支持false。默认为false
numInitialChunksinteger整数

Optional. 可选。Specifies the initial number of chunks to create across all shards in the cluster when resharding a collection. 指定重新分片集合时在集群中的所有分片上创建的初始块数。The default value is 90. MongoDB will then create and balance chunks across the cluster. The numInitialChunks must result in less than 8192 per shard.默认值为90。然后,MongoDB将在整个集群中创建和平衡块。numInitialChunks的结果必须小于每个分片8192个。

If you see the error "The shard key provided does not have enough cardinality to make the required number of chunks", reshard the collection with a lower numInitialChunks value.如果您看到错误“提供的分片键没有足够的基数来生成所需数量的块”,请使用较低的numInitialChunks值重新分片集合。

Starting in MongoDB 8.2, resharding operations ignore the numInitialChunks setting when the shard key contains a hashed prefix. 从MongoDB 8.2开始,当分片键包含哈希前缀时,重新分片操作会忽略numInitialChunks设置。Instead, MongoDB deterministically splits the hashed key space among recipients, using the same approach as initial chunk creation for empty hashed collections.相反,MongoDB使用与空哈希集合的初始块创建相同的方法,在接收者之间确定性地分割哈希键空间。

collationdocument文档Optional. 可选。If the collection specified in reshardCollection has a default collation, you must include a collation document with { locale : "simple" }, or the reshardCollection command fails.如果reshardCollection中指定的集合具有默认排序规则,则必须包含具有{ locale : "simple" }的排序规则文档,否则reshardCollection命令将失败。
zonesarray数组

Optional. 可选。Specifies the zones for the collection.指定集合的区域。

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
},
...
]
forceRedistributionboolean布尔值

Optional. 可选。If set to true, the operation runs even if the new shard key is the same as the old shard key. Use with the zones option to move data to specific zones.如果设置为true,即使新分片键与旧分片键相同,操作也会运行。与zones选项一起使用可将数据移动到特定区域。

New in version 8.0.在版本8.0中新增。

demoModeboolean布尔值Optional. 可选。Allows resharding operations to complete quickly. When set to true, demoMode bypasses the minimum resharding operation duration of 5 minutes. 允许快速完成重新分片操作。当设置为true时,demoMode会绕过5分钟的最小重新分片操作持续时间。You can use this parameter for resharding testing or demonstration purposes.您可以将此参数用于重新分片测试或演示目的。

Considerations注意事项

Index builds that occur during resharding might silently fail.在重新分级过程中发生的索引构建可能会悄无声息地失败。

  • Do not create indexes during the resharding process.在重新评分过程中不要创建索引。
  • Do not start the resharding process if there are ongoing index builds.如果有正在进行的索引构建,请不要启动重新分片过程。

If the collection you're resharding uses MongoDB Search, the search index becomes unavailable when the resharding operation completes. You need to manually rebuild the search index once the resharding operation completes.如果您正在重新分片的集合使用MongoDB搜索,则重新分片操作完成后,搜索索引将不可用。重新分片操作完成后,您需要手动重建搜索索引。

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 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.在初始化阶段,重新分片协调器确定分片集合的新数据分布。

Clone Phase克隆阶段

During the clone phase:在克隆阶段:

  • Each recipient shard creates a temporary, empty sharded collection with the same collection options as the donor sharded collection. This new collection is the target for where recipient shards write the new data. The recipient shards do not create any index except the _id index until the index phase.每个接收者分片都会创建一个临时的空分片集合,其集合选项与捐赠者分片集合相同。这个新集合是收件人分片写入新数据的目标。在索引阶段之前,接收方分片不会创建除_id索引之外的任何索引。
  • Each recipient shard clones collection data from the donor shard, including all documents that the recipient shard owns under the new shard key.每个接收者分片克隆来自捐赠者分片的集合数据,包括接收者分片在新分片键下拥有的所有文档。

Index Phase指数阶段

During the index phase, each recipient shard 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.在索引阶段,每个接收者分片都会构建必要的新索引。这些包括分片集合上的所有现有索引,以及与新分片键模式兼容的索引(如果分片集合中不存在这样的索引)。

Apply and Catch-up Phase申请和追赶阶段

Changed in version 8.2.在版本8.2中的更改。

During the apply and catch-up phase:在申请和追赶阶段:

  • Each recipient shard begins applying oplog entries that were written to the the corresponding donor shard after the recipient cloned the data.每个接收者分片开始应用oplog条目,这些条目是在接收者克隆数据后写入相应的捐赠者分片的。
  • When the estimate for the time remaining to complete the resharding operation is under 500 ms, the donor shard blocks writes on the source collection.当完成重新分片操作的剩余时间估计在500毫秒以下时,供体分片块会在源集合上写入。

Note

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

Commit Phase提交阶段

During the commit phase:在提交阶段:

  • The resharding coordinator waits for all shards to reach strict consistency, then commits the resharding operation.重新分片协调器等待所有分片达到严格一致性,然后提交重新分片操作。
  • 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.每个捐赠者的分片都会丢弃旧的分片集合。

Note

Once the resharding process reaches the commit phase, the process cannot be ended with abortReshardCollection.一旦重新分片过程到达提交阶段,该过程就不能以abortReshardCollection结束。

Examples示例

Reshard a 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 }
})

Output:输出:

{
ok: 1,
'$clusterTime': {
clusterTime: Timestamp(1, 1624887954),
signature: {
hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
keyId: 0
}
},
operationTime: Timestamp(1, 1624887947)
}

Redistribute Data to New Shards将数据重新分配到新分片

Starting in MongoDB 8.0, you can reshard a collection on the same key, which can be used to redistribute data onto new shards.从MongoDB 8.0开始,您可以在同一键上重新分片一个集合,这可用于将数据重新分发到新的分片上。

After adding a shard to the cluster, you use the reshardCollection command with the forceRedistribution option to redistribute data across the cluster:在将分片添加到集群后,您可以使用带有forceRedistribution选项的reshardCollection命令在集群中重新分发数据:

db.adminCommand({
reshardCollection: "accounts.invoices",
key: { store_id: "hashed" },
forceRedistribution: true
})

Redistribute Data to Different Zones将数据重新分配到不同的区域

Starting in MongoDB 8.0, you can use the reshardCollection command to move data into new zones without changing the shard key.从MongoDB 8.0开始,您可以使用reshardCollection命令将数据移动到新区域,而无需更改分片键。

The following command redistributes data for the accounts.sales collection using the same shard key, moving data to the shards associated with zones zone04 and zone05:以下命令使用相同的分片键重新分发accounts.sales集合的数据,将数据移动到与区域zone04zone05关联的分片:

db.adminCommand({
reshardCollection: "accounts.sales",
key: { region_id: "hashed" },
forceRedistribution: true,
zones: [
{
zone: "zone04",
min: { region_id: MinKey() },
max: { region_id: 10 }
},
{
zone: "zone05",
min: { region_id: 10 },
max: { region_id: MaxKey() }
}
]
})

Learn More了解更多