Database Manual / Reference / Database Commands / Administration

autoCompact (database command数据库命令)

Definition定义

autoCompact

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

Enables or disables background compaction. When enabled, autoCompact periodically iterates through all available files and continuously runs compaction if there is enough free storage space available.启用或禁用背景压缩。启用后,autoCompact会定期迭代所有可用文件,并在有足够的可用存储空间的情况下连续运行压缩。

Before you enable autoCompact, run the $collStats aggregation stage with the storageStats: {} 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 freeStorageSize is less than freeSpaceTargetMB, 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:该命令可以接受以下可选字段:

Field字段Type类型Description描述
freeSpaceTargetMBInteger整数

Optional. 可选。Specifies the minimum amount of storage space, in megabytes, that must be recoverable for compaction to proceed.指定压缩必须可恢复的最小存储空间量(以兆字节为单位)。

Default:默认值: 20

runOnceboolean布尔值

Optional. 可选。If runOnce is set to true, background compaction runs only once through every collection on the node.如果runOnce设置为true,则后台压缩仅在节点上的每个集合中运行一次。

If runOnce is set to false, background compaction runs continuously on all collections in the database. 如果runOnce设置为false,则后台压缩将在数据库中的所有集合上连续运行。If a collection fails to compact while runOnce is false, MongoDB temporarily skips that collection and continues compacting the remaining collections. MongoDB attempts to compact the failed collection again after approximately one hour.如果runOncefalse时某个集合压缩失败,MongoDB会暂时跳过该集合并继续压缩其余集合。大约一小时后,MongoDB试图再次压缩失败的集合。

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 autoCompact command 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

Learn More了解更多