Database Manual / Reference / Database Commands / Sharding

configureCollectionBalancing (database command数据库命令)

Definition定义

configureCollectionBalancing

New in version 5.3.在版本5.3中新增。

Configures balancer settings for a sharded collection, such as setting the chunk size for and defragmenting the collection.配置分片集合的平衡器设置,例如设置集合的块大小和对集合进行分片整理。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.adminCommand(
{
configureCollectionBalancing: "<db>.<collection>",
chunkSize: <num>,
defragmentCollection: <bool>,
enableAutoMerger: <bool>,
enableBalancing: <bool>
}
)

Command Fields命令字段

configureCollectionBalancing has the following fields:具有以下字段:

Field字段Type类型Necessity必要性Description描述
configureCollectionBalancingstring字符串Required必需The name of the database and sharded collection to configure.要配置的数据库和分片集合的名称。
chunkSizeinteger整数Optional可选Sets the chunk size in MiB for the collection. The recommended size is 256, 512, or larger. For details on default behavior, see Default Behavior When chunkSize Is Not Specified.设置集合的块大小(MiB)。建议大小为256、512或更大。有关默认行为的详细信息,请参阅未指定chunkSize时的默认行为
defragmentCollectionboolean布尔值Optional可选Causes the balancer to defragment the collection. Defaults to false.使平衡器对集合进行分片整理。默认为false
enableAutoMergerboolean布尔值Optional可选Sets whether the AutoMerger takes this collection into account. Defaults to true.设置AutoMerger是否考虑此集合。默认为true
enableBalancingboolean布尔值Optional可选

Sets whether the balancer operates on the collection. When set to false, the balancer is disabled for the collection.设置平衡器是否对集合进行操作。当设置为false时,该集合的平衡器将被禁用。

New in version 8.1.在版本8.1中新增。

For more information, see Data Partitioning with Chunks.有关更多信息,请参阅使用块进行数据分区

To configure the chunk defragmentation throttling time parameter, see chunkDefragmentationThrottlingMS.要配置块分片整理限制时间参数,请参阅chunkDefragmentationThrottlingMS

To learn about defragmenting sharded collections, see Defragment Sharded Collections.要了解分片化集合的分片整理,请参阅分片化集合

Behavior行为

Default Behavior When chunkSize Is Not Specified未指定chunkSize时的默认行为

If you do not specify chunkSize for a collection and no custom size has been set previously, the global default chunkSize is used for balancing.如果您没有为集合指定chunkSize,并且之前没有设置自定义大小,则使用全局默认chunkSize进行平衡。

Specifying指定 chunkSize:0

If you use configureCollectionBalancing with chunkSize: 0, the per-collection chunkSize is reset and the global default chunkSize is used for balancing.如果将configureCollectionBalancingchunkSize:0一起使用,则重置每个集合的chunkSize,并使用全局默认chunkSize进行平衡。

For more information on configuring default chunkSize, see Modify Range Size in a Sharded Cluster.有关配置默认chunkSize的更多信息,请参阅修改分片群集中的范围大小

Default Behavior When enableAutoMerger Is Not Specified未指定启用AutoMerger时的默认行为

If you do not specify enableAutoMerger for a collection and no custom automerging behavior has been previously set, it defaults to true and will be taken into account by the AutoMerger.如果您没有为集合指定enableAutoMerger,并且之前没有设置自定义自动合并行为,则默认为trueAutoMerger会将其考虑在内。

Parameter Validation参数验证

Starting in 8.2, MongoDB validates the spelling of parameters that you pass to configureCollectionBalancing. If you misspell a parameter, configureCollectionBalancing fails to execute and returns an error.从8.2开始,MongoDB会验证您传递给configureCollectionBalancing的参数的拼写。如果参数拼写错误,configureCollectionBalancing将无法执行并返回错误。

Examples示例

Configure Chunk Size配置块大小

To change the chunk size for a sharded collection, use the chunkSize option:要更改分片集合的块大小,请使用chunkSize选项:

db.adminCommand( {
configureCollectionBalancing: "test.students",
chunkSize: 256
} )

Use this command to change the chunk size for the given collection.使用此命令更改给定集合的块大小。

Warning

By default, MongoDB cannot move a chunk if the number of documents in the chunk is greater than 2 times the result of dividing the configured chunk size by the average document size.默认情况下,如果块中的文档数量大于配置块大小除以平均文档大小的结果的2倍,则MongoDB无法移动块。

To find the average document size, see the avgObjSize field in the output of the db.collection.stats() method.要查找平均文档大小,请参阅db.collection.stats()方法输出中的avgObjSize字段。

For more information, see Range Size.有关详细信息,请参阅范围大小

Defragment Collections分片整理集合

Warning

We do not recommend using defragmentCollection to defragment sharded collections for MongoDB 6.0.0 to 6.0.3 and MongoDB 6.1.0 to 6.1.1, as the defragmentation process on these releases can make databases and collections unavailable for extended periods of time.我们不建议使用defragmentCollection对MongoDB 6.0.0至6.0.3和MongoDB 6.1.0至6.1.1的分片集合进行分片整理,因为这些版本的分片整理过程可能会使数据库和集合长时间不可用。

To tell the balancer to defragment a sharded collection, use the defragmentCollection option:要告诉平衡器对分片集合进行分片整理,请使用defragmentCollection选项:

db.adminCommand( {
configureCollectionBalancing: "test.students",
defragmentCollection: true
} )

Use this command to have the balancer defragment a sharded collection. To monitor the chunk defragmentation process, use the balancerCollectionStatus command.使用此命令让平衡器对分片集合进行分片整理。要监视块分片整理过程,请使用balancerCollectionStatus命令。

To learn more about defragmenting sharded collections, see Defragment Sharded Collections.要了解有关对分片集合进行分片整理的更多信息,请参阅分片集合分片整理

Reconfigure and Defragment Collections重新配置和分片整理集合

To defragment a sharded collection while updating the chunk size, use the defragmentCollection option and the chunkSize option together:要在更新块大小时对分片集合进行分片整理,请同时使用defragmentCollection选项和chunkSize选项:

db.adminCommand( {
configureCollectionBalancing: "test.students",
chunkSize: 512,
defragmentCollection: true
} )

Disable the AutoMerger on a Collection禁用集合上的自动合并

To explicitly disable the AutoMerger on a collection, set the enableAutoMerger option to false:要明确禁用集合上的AutoMerger,请将enableAutoMerger选项设置为false

db.adminCommand( {
configureCollectionBalancing: "test.students",
enableAutoMerger: false
} )

Disable Balancing on a Collection禁用集合上的平衡

To explicitly disable the balancer on a collection, set the enableBalancing option to false:要显式禁用集合上的平衡器,请将enableBalancing选项设置为false

db.adminCommand( {
configureCollectionBalancing: "test.students",
enableBalancing: false
} )