Definition定义
configureCollectionBalancingNew 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:具有以下字段:
configureCollectionBalancing | |||
chunkSize | chunkSize时的默认行为。 | ||
defragmentCollection | false.false。 | ||
enableAutoMerger | true.AutoMerger是否考虑此集合。默认为true。 | ||
enableBalancing |
|
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.configureCollectionBalancing与chunkSize: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,并且之前没有设置自定义自动合并行为,则默认为true,AutoMerger会将其考虑在内。
Parameter Validation参数验证
Starting in 8.2, MongoDB validates the spelling of parameters that you pass to 从8.2开始,MongoDB会验证您传递给configureCollectionBalancing. If you misspell a parameter, configureCollectionBalancing fails to execute and returns an error.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
} )