Change a Shard Key更改分片键
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 uneven data distribution and the following problems:次优的分片键可能会导致数据分布不均匀,并出现以下问题:
To address these issues, MongoDB allows you to change your shard key:为了解决这些问题,MongoDB允许您更改分片键:
Starting in MongoDB 5.0, you can reshard a collection by changing a collection's shard key.从MongoDB 5.0开始,您可以通过更改集合的分片键来重新分片集合。Starting in MongoDB 4.4, you can refine a shard key by adding a suffix field or fields to the existing shard key.从MongoDB 4.4开始,您可以通过在现有的分片键中添加一个或多个后缀字段来细化分片键。
Data distribution fixes are most effective when you reshard a collection. 数据分发修复程序在重新分片集合时最有效。If you want to improve data distribution and your cluster meets the criteria to reshard, you should reshard the collection instead of refining the shard key.如果您想改善数据分布,并且集群符合重新分片的标准,则应该重新分片集合,而不是细化分片键。 If your cluster doesn't meet the criteria to reshard, you should refine the shard key.如果您的集群不符合重新分片的条件,则应该细化分片关键帧。
In MongoDB 4.2 and earlier, a document's shard key is immutable.在MongoDB 4.2及更早版本中,文档的分片键是不可变的。
For more information on common performance and scaling issues and advice on how to fix them, read Troubleshoot Shard Keys.有关常见性能和扩展问题的更多信息以及如何解决这些问题的建议,请阅读分片键疑难解答。