On this page本页内容
_id
Values_id
值New in version 5.0.在版本5.0中新增。
The ideal shard key allows MongoDB to distribute documents evenly throughout the cluster while facilitating common query patterns. 理想的分片键允许MongoDB在整个集群中均匀分布文档,同时促进常见的查询模式。A suboptimal shard key can lead to performance or scaling issues due to uneven data distribution. 由于数据分布不均匀,次优的分片键可能会导致性能或缩放问题。Starting in MongoDB 5.0, you can change the shard key for a collection to change the distribution of your data across a cluster.从MongoDB 5.0开始,您可以更改集合的shard键,以更改数据在集群中的分布。
Before resharding your collection, read Troubleshoot Shard Keys for information on common performance and scaling issues and advice on how to fix them.在重新分片集合之前,请阅读分片键疑难解答,了解常见性能和扩展问题的信息,以及如何修复这些问题的建议。
Before you reshard your collection, ensure that you meet the following requirements:在重新存储集合之前,请确保满足以下要求:
Your database meets these resource requirements:您的数据库满足以下资源要求:
These requirements are not enforced by the database. 数据库不强制执行这些要求。A failure to allocate enough resources can result in:未能分配足够的资源可能导致:
If your application has time periods with less traffic, reshard your collection during that time if possible.如果您的应用程序具有流量较少的时间段,请尽可能在该时间段内重新硬存储您的集合。
You have rewritten your application code to update your queries to use both the current shard key and the new shard key.您已经重写了应用程序代码,以更新查询,使其同时使用当前的分片键和新的分片键。
The following queries return an error if the query filter does not include both the current shard key or a unique field (like 如果查询筛选器不同时包含当前分片键或唯一字段(如_id
):_id
),则以下查询将返回错误:
deleteOne()
findAndModify()
findOneAndDelete()
findOneAndReplace()
findOneAndUpdate()
replaceOne()
updateOne()
For optimal performance, we recommend that you also rewrite other queries to include the new shard key.为了获得最佳性能,我们建议您也重写其他查询以包含新的shard键。
Once the resharding operation completes, you can remove the old shard key from the queries.一旦重新加密操作完成,就可以从查询中删除旧的shard密钥。
No index builds are in progress. 没有正在进行的索引生成。Use 使用db.currentOp()
to check for any running index builds:db.currentOp()
检查是否有任何正在运行的索引生成:
db.adminCommand( { currentOp: true, $or: [ { op: "command", "command.createIndexes": { $exists: true } }, { op: "none", "msg" : /^Index Build/ } ] } )
In the result document, if the 在结果文档中,如果inprog
field value is an empty array, there are no index builds in progress:inprog
字段值为空数组,则没有正在进行的索引生成:
{ inprog: [], ok: 1, '$clusterTime': { ... }, operationTime: <timestamp> }
We strongly recommend that you check the Limitations and read the resharding process section in full before resharding your collection.我们强烈建议您在重新分片您的集合之前,检查限制并阅读完整的重分片过程部分。
writeConcernMajorityJournalDefault
true
.The following commands and corresponding shell methods are not supported on the collection that is being resharded while the resharding operation is in progress:正在进行重新存储操作时,正在重新存储的集合不支持以下命令和相应的shell方法:
The following commands and methods are not supported on the cluster while the resharding operation is in progress:正在进行重新存储操作时,群集上不支持以下命令和方法:
Using any of the preceding commands during a resharding operation causes the resharding operation to fail.在重新存储操作期间使用上述任何命令都会导致重新存储操作失败。
While connected to the 连接到mongos
, issue a reshardCollection
command that specifies the collection to be resharded and the new shard key:mongos
时,发出reshardCollection
命令,指定要重新存储的集合和新的分片键:
db.adminCommand({ reshardCollection: "<database>.<collection>", key: <shardkey> })
MongoDB sets the max number of seconds to block writes to two seconds and begins the resharding operation.MongoDB将阻塞写入的最大秒数设置为2秒,并开始重新存储操作。
To monitor the resharding operation, you can use the 要监视重新存储操作,可以使用$currentOp
pipeline stage:$currentOp
管道阶段:
db.getSiblingDB("admin").aggregate([ { $currentOp: { allUsers: true, localOps: false } }, { $match: { type: "op", "originatingCommand.reshardCollection": "<database>.<collection>" } } ])
To see updated values, you need to continuously run the preceeding pipeline.要查看更新的值,需要连续运行前面的管道。
The $currentOp
pipeline outputs:$currentOp
管道输出:
totalOperationTimeElapsedSecs
remainingOperationTimeEstimatedSecs
[ { shard: '<shard>', type: 'op', desc: 'ReshardingRecipientService | ReshardingDonorService | ReshardingCoordinatorService <reshardingUUID>', op: 'command', ns: '<database>.<collection>', originatingCommand: { reshardCollection: '<database>.<collection>', key: <shardkey>, unique: <boolean>, collation: { locale: 'simple' } }, totalOperationTimeElapsedSecs: <number>, remainingOperationTimeEstimatedSecs: <number>, ... }, ... ]
Throughout the resharding process, the estimated time to complete the resharding operation (在整个重新装载过程中,完成重新装载操作的估计时间(剩余remainingOperationTimeEstimatedSecs
) decreases. remainingOperationTimeEstimatedSecs
)减少。When the estimated time is below two seconds, MongoDB blocks writes and completes the resharding operation. 当估计时间低于2秒时,MongoDB阻塞写入并完成重新存储操作。Until the estimated time to complete the resharing operation is below two seconds, the resharding operation does not block writes by default. 在完成重新存储操作的估计时间低于两秒之前,默认情况下,重新存储操作不会阻止写入。During the time period where writes are blocked your application experiences an increase in latency.在写操作被阻止的时间段内,应用程序的延迟会增加。
Once the resharding process has completed, the resharding command returns 重分片过程完成后,重分片命令返回ok: 1
.ok:1
。
{ ok: 1, '$clusterTime': { clusterTime: <timestamp>, signature: { hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), keyId: <number> } }, operationTime: <timestamp> }
To see whether the resharding operation completed successfully, check the output of the 要查看重新存储操作是否成功完成,请检查sh.status()
method:sh.status()
方法的输出:
sh.status()
The sh.status()
method output contains a subsection for the databases
. sh.status()
方法输出包含数据库的子部分。If resharding has completed successfully, the output lists the new shard key for the collection:如果重新存储成功完成,输出将列出集合的新分片键:
databases [ { database: { _id: '<database>', primary: '<shard>', partitioned: true, version: { uuid: <uuid>, timestamp: <timestamp>, lastMod: <number> } }, collections: { '<database>.<collection>': { shardKey: <shardkey>, unique: <boolean>, balancing: <boolean>, chunks: [], tags: [] } } } ... ]
If the resharded collection uses Atlas Search, the search index will become unavailable when the resharding operation completes. 如果重新存储的集合使用Atlas Search,则当重新存储操作完成时,搜索索引将不可用。You need to manually rebuild the search index once the resharding operation completes.重新存储操作完成后,需要手动重建搜索索引。
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
命令会阻止提前写入,并强制完成重新存储操作。The command has the following syntax:该命令具有以下语法:
db.adminCommand({ commitReshardCollection: "<database>.<collection>" })
You can abort the resharding operation during any stage of the resharding operation, even after running the 您可以在重新存储操作的任何阶段中止重新存储操作,即使在运行commitReshardCollection
, until shards have fully caught up.commitReshardCollection
之后,直到分片完全被捕获。
For example, if the write unavailability duration estimate does not decrease, you can abort the resharding operation with the 例如,如果写入不可用持续时间估计值没有减少,可以使用abortReshardCollection
command:abortReshardCollection
命令中止重新存储操作:
db.adminCommand({ abortReshardCollection: "<database>.<collection>" })
After canceling the operation, you can retry the resharding operation during a time window with lower write volume. 取消操作后,可以在写入量较低的时间窗口内重试重新存储操作。If this is not possible, add more shards before retrying.如果不可能,请在重试之前添加更多分片。
The minimum duration of a resharding operation is always 5 minutes.重新装货操作的最短持续时间始终为5分钟。
Retryable writes initiated before or during resharding can be retried during and after the collection has been resharded for up to 5 minutes. 在重新存储之前或期间启动的可重试写入操作可以在集合重新存储期间和之后重试,最多5分钟。After 5 minutes you may be unable to find the definitive result of the write and subsequent attempts to retry the write fail with an 5分钟后,您可能无法找到写入的最终结果,随后尝试重试写入失败,并出现IncompleteTransactionHistory
error.IncompleteTransactionHistory
错误。
If a primary failover on a replica set shard or config server occurs, the resharding operation aborts.如果副本集分片或配置服务器上发生主故障转移,则重新存储操作将中止。
If a resharding operation aborts due to a primary failover, run the 如果重新存储操作由于主故障转移而中止,请在启动新的重新存储操作之前运行cleanupReshardCollection
command before starting a new resharding operation:cleanupReshardCollection
命令:
db.runCommand({ cleanupReshardCollection: "<database>.<collection>" })
_id
Values_id
值The resharding operation fails if 如果_id
values are not globally unique to avoid corrupting collection data. _id
值不是全局唯一的,则重新存储操作失败,以避免损坏集合数据。Duplicate 重复的_id
values can also prevent successful chunk migration. _id
值也会阻止成功的块迁移。If you have documents with duplicate 如果有具有重复_id
values, copy the data from each into a new document, and then delete the duplicate documents._id
值的文档,请将每个文档中的数据复制到新文档中,然后删除重复的文档。