Docs HomeMongoDB Manual

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. 若要手动拆分块,请使用split命令,并将字段置于middlefind中。mongosh provides the helper methods sh.splitFind() and sh.splitAt().

splitFind() splits the chunk that contains the first document returned that matches this query into two equally sized chunks. splitFind()将包含返回的与此查询匹配的第一个文档的块拆分为两个大小相等的块。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:以下命令拆分records数据库的people集合中包含zipcode字段值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.拆分发生在与查询匹配的文档的位置,而不管该文档在块中的位置如何。

Tip

See also: 另请参阅:

Empty Collection空集合