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. middle或find的 split命令。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.拆分发生在与查询匹配的文档的位置,而不管该文档在块中的位置如何。