Definition定义
autoCompactNew in version 8.0.在版本8.0中新增。Enables or disables background compaction. When enabled,启用或禁用背景压缩。启用后,autoCompactperiodically iterates through all available files and continuously runs compaction if there is enough free storage space available.autoCompact会定期迭代所有可用文件,并在有足够的可用存储空间的情况下连续运行压缩。Before you enable在启用autoCompact, run the$collStatsaggregation stage with thestorageStats: {}option to see if you have enough available storage space for compaction to proceed.autoCompact之前,请使用storageStats:{}选项运行$collStats聚合阶段,查看是否有足够的可用存储空间进行压缩。If the amount of available space returned by如果freeStorageSizeis less thanfreeSpaceTargetMB, background compaction has no effect.freeStorageSize返回的可用空间量小于freeSpaceTargetMB,则后台压缩无效。
Syntax语法
The command has the following syntax:该命令具有以下语法:
db.runCommand(
{
autoCompact: <boolean>,
freeSpaceTargetMB: <int>, // Optional
runOnce: <boolean>, // Optional
}
)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部署的完全托管服务
Important
This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.M0和Flex集群不支持此命令。有关详细信息,请参阅不支持的命令。
- 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的源代码可用、免费使用和自我管理版本
Command Fields命令字段
The command can take the following optional fields:该命令可以接受以下可选字段:
freeSpaceTargetMB |
| |
runOnce |
|
Required Privileges所需权限
For clusters enforcing authentication, you must authenticate as a user with the 对于强制身份验证的集群,您必须以具有集群上compact privilege action on the cluster. compact(紧凑)权限操作的用户身份进行身份验证。The hostManager role provides the required privileges for running autoCompact.hostManager角色提供运行autoCompact所需的权限。
Behavior行为
Blocking阻塞
Although the 虽然autoCompact command itself doesn't block reads and writes, background compaction applies the same blocking behavior as the compact command.autoCompact命令本身不会阻止读写,但后台压缩应用了与compact命令相同的阻止行为。
Additionally, MongoDB returns an error if you call 此外,如果在后台压缩处于活动状态时调用autoCompact while background compaction is active. If you need to restart autoCompact or run it again with different options, you must first stop the current background compaction operation:autoCompact,MongoDB将返回错误。如果需要重新启动autoCompact或使用其他选项再次运行它,则必须首先停止当前的后台压缩操作:
db.runCommand( { autoCompact: false } )
Once the current background compaction is disabled, you can restart 禁用当前后台压缩后,您可以使用新配置重新启动autoCompact with the new configuration.autoCompact。
Excluded Collections排除的集合
If an oplog exists, MongoDB excludes it from background compaction.如果oplog存在,MongoDB会将其从后台压缩中排除。
Performance Considerations性能注意事项
We recommend running 我们建议在低流量期间运行autoCompact during periods of low traffic.autoCompact。
Compaction regularly checkpoints the database, which can lead to synchronization overhead. On high-traffic databases, this can potentially delay or prevent operational tasks such as taking backups. To avoid unexpected disruptions, disable compaction before taking a backup.压缩会定期检查数据库,这可能会导致同步开销。在高流量数据库上,这可能会延迟或阻止备份等操作任务。为避免意外中断,请在备份前禁用压缩。
Replica Sets复制集
You can run background compaction on collections and indexes that are stored in a replica set. However, note the following considerations:您可以对存储在副本集中的集合和索引运行后台压缩。但是,请注意以下注意事项:
The primary node does not replicate the主节点不会将autoCompactcommand to the secondary nodes.autoCompact命令复制到辅助节点。A secondary node can replicate data while background compaction is running.辅助节点可以在后台压缩运行时复制数据。Reads and writes are permitted while background compaction is running.后台压缩运行时允许读取和写入。
Sharded Clusters分片集群
autoCompact only applies to 仅适用于mongod instances. In a sharded environment, run autoCompact on each shard separately.mongod实例。在分片环境中,分别在每个分片上运行autoCompact。
You cannot run 您不能对autoCompact against a mongos instance.mongos实例运行autoCompact。