Database Manual / Sharding / Data Partitioning

Split Chunks in a Sharded Cluster分片群集中的分裂块

By default, MongoDB potentially splits a chunk only when migrating data belonging to it. 默认情况下,MongoDB可能仅在迁移属于它的数据时才拆分However, you may want to split chunks manually if you have a large amount of data in your cluster and very few chunks, as is the case after deploying a cluster using existing data.但是,如果集群中有大量数据而很少,则可能需要手动拆分块,就像使用现有数据部署集群后的情况一样。

To split chunks manually, use the split command with either fields middle or find. 要手动分割块,请使用带有字段middlefindsplit命令。mongosh provides the helper methods sh.splitFind() and sh.splitAt().提供了辅助方法sh.splitFind()sh.splitAt()

splitFind() splits the chunk that contains the first document returned that matches this query into two equally sized chunks. 将包含与此查询匹配的第一个返回文档的块拆分为两个大小相等的块。You must specify the full namespace (i.e. "<database>.<collection>") of the sharded collection to splitFind(). 您必须指定分片集合的完整命名空间(即“<database>.<collection>”)才能splitFind()The query in splitFind() does not need to use the shard key, though it nearly always makes sense to do so.splitFind()中的查询不需要使用分片键,尽管这样做几乎总是有意义的。

Example示例

The following command splits the chunk that contains the value of 63109 for the zipcode field in the people collection of the records database:以下命令拆分包含记录数据库people集合中邮政编码字段值63109的块:

sh.splitFind( "records.people", { "zipcode": "63109" } )

Use splitAt() to split a chunk in two, using the queried document as the lower bound in the new chunk:使用splitAt()将一个块一分为二,将查询到的文档作为新块的下限:

Example示例

The following command splits the chunk that contains the value of 63109 for the zipcode field in the people collection of the records database.以下命令拆分包含records数据库people合中zipcode字段值63109的块。

sh.splitAt( "records.people", { "zipcode": "63109" } )

Note

splitAt() does not necessarily split the chunk into two equally sized chunks. 不一定将块分成两个大小相等的块。The split occurs at the location of the document matching the query, regardless of where that document is in the chunk.拆分发生在与查询匹配的文档的位置,而不管该文档在块中的位置如何。