Definition定义
reshardCollectionThereshardCollectioncommand 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 thesh.reshardCollection()helper method.mongosh中,此命令也可以通过sh.reshardCollection()辅助方法运行。Helper methods are convenient for助手方法对mongoshusers, 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:该命令包含以下字段:
reshardCollection | <database>.<collection>.<database>.<collection>的形式。 | |
key |
| |
unique | false is supported. Defaults to false.false。默认为false。 | |
numInitialChunks |
| |
collation | reshardCollection has a default collation, you must include a collation document with { locale : "simple" }, or the reshardCollection command fails.reshardCollection中指定的集合具有默认排序规则,则必须包含具有{ locale : "simple" }的排序规则文档,否则reshardCollection命令将失败。 | |
zones |
| |
forceRedistribution |
| |
demoMode | demoMode bypasses the minimum resharding operation duration of 5 minutes. true时,demoMode会绕过5分钟的最小重新分片操作持续时间。 |
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每个接收者分片都会创建一个临时的空分片集合,其集合选项与捐赠者分片集合相同。这个新集合是收件人分片写入新数据的目标。在索引阶段之前,接收方分片不会创建除_idindex 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 从MongoDB 8.0开始,您可以使用reshardCollection command to move data into new zones without changing the shard key.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集合的数据,将数据移动到与区域zone04和zone05关联的分片:
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() }
}
]
})