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
命令,并将字段置于middle
或find
中。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()
中的查询不需要使用分片键,尽管这样做几乎总是有意义的。
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()
将区块一分为二,将查询到的文档用作新区块中的下限:
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" } )
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.拆分发生在与查询匹配的文档的位置,而不管该文档在块中的位置如何。