定义
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
注意
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
db.adminCommand( { configureCollectionBalancing: "<db>.<collection>", chunkSize: <num>, defragmentCollection: <bool>, enableAutoMerger: <bool>, enableBalancing: <bool> } )
命令字段
configureCollectionBalancing 有以下字段:
字段 | 类型 | 必要性 | 说明 |
|---|---|---|---|
| 字符串 | 必需 | 要配置的数据库和分片collection的名称。 |
| 整型 | 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. |
| 布尔 | Optional | 导致负载均衡器对集合进行碎片整理。 默认为 |
| 布尔 | Optional | 设置AutoMerger是否考虑此集合。默认为 |
| 布尔 | Optional | 设置负载均衡器是否对集合进行操作。当设立为 8.1版本新增。 |
有关更多信息,请参阅使用数据块进行数据分区。
要配置数据块碎片整理限制时间参数,请参阅chunkDefragmentationThrottlingMS 。
要了解如何对分片集合进行碎片整理,请参阅对分片集合进行碎片整理。
行为
未指定 chunkSize 时的默认行为
如果您没有为collection指定chunkSize ,也没有设置自定义大小,则使用全局默认chunkSize进行负载均衡。
指定 chunkSize: 0
If you use configureCollectionBalancing with chunkSize: 0, the per-collection chunkSize is reset and the global default chunkSize is used for balancing.
有关配置默认chunkSize的更多信息,请参阅修改分片集群中的范围大小。
未指定 enableAutoMerger 时的默认行为
如果您没有为collection指定enableAutoMerger ,并且以前没有设置自定义自动合并行为,则defaults为true ,并且 AutoMerger 将予以考虑。
Parameter Validation
从 8.2 开始,MongoDB会验证您传递给 configureCollectionBalancing 的参数的拼写。如果参数拼写错误,configureCollectionBalancing 将无法执行并返回错误。
示例
配置数据块大小
要更改分片collection的数据块大小,请使用chunkSize选项:
db.adminCommand( { configureCollectionBalancing: "test.students", chunkSize: 256 } )
使用此命令可更改给定collection的数据块大小。
警告
默认,如果数据段中的数据块数据块大于配置的数据数据块大小除以平均文档大小所得结果的2倍,则MongoDB无法移动该数据段。
要查找平均文档大小,请参阅db.collection.stats()方法输出中的avgObjSize字段。
有关详细信息,请参阅范围大小。
对collection进行碎片整理
警告
我们不建议使用defragmentCollection对 MongoDB 6.0.0 到 6.0.3 以及 MongoDB 6.1.0 的collection进行碎片整理至 6.1.1、 因为这些版本上的碎片整理过程可能会导致数据库和集合长时间不可用。
要指示负载均衡器对分片collection进行碎片整理,请使用defragmentCollection选项:
db.adminCommand( { configureCollectionBalancing: "test.students", defragmentCollection: true } )
使用此命令可让负载均衡器对分片collection进行碎片整理。要监控数据段碎片整理进程,请使用balancerCollectionStatus命令。
要了解有关对分片collection进行碎片整理的更多信息,请参阅对分片collection进行碎片整理。
重新配置collection并进行碎片整理
要在更新数据段大小时对分片集合进行碎片整理,请同时使用defragmentCollection选项和chunkSize选项:
db.adminCommand( { configureCollectionBalancing: "test.students", chunkSize: 512, defragmentCollection: true } )
禁用collection上的 AutoMerger
要显式禁用collection上的 AutoMerger,请将enableAutoMerger选项设置为false :
db.adminCommand( { configureCollectionBalancing: "test.students", enableAutoMerger: false } )
对集合禁用负载均衡
要显式禁用集合上的负载均衡器,请将 enableBalancing 选项设立为 false:
db.adminCommand( { configureCollectionBalancing: "test.students", enableBalancing: false } )