compact

On this page本页内容

Definition定义

compact

Rewrites and defragments all data and indexes in a collection. 重写并整理集合中的所有数据和索引。On WiredTiger databases, this command will release unneeded disk space to the operating system.WiredTiger数据库上,此命令将向操作系统释放不需要的磁盘空间。

compact has the following form:具有以下形式:

{ compact: <collection name> }

compact takes the following fields:采用以下字段:

Note注意
Starting in MongoDB 4.2从MongoDB 4.2开始

MongoDB removes the MMAPv1 storage engine and the MMAPv1 specific options paddingFactor, paddingBytes, preservePadding for compact.MongoDB删除了MMAPv1存储引擎和特定于MMAPv1的选项paddingFactorpaddingBytespreservePpadding以实现compact

Field字段Type类型Description描述
compactstringThe name of the collection.集合的名称。
forceflag

Changed in version 4.4.在版本4.4中更改

Optional. 可选。Starting in v4.4, if specified, forces compact to run on the primary in a replica set. 从v4.4开始(如果指定),强制compact副本集中的primary上运行。Before v4.4, this boolean field enabled compact to run on the primary in a replica set if the value was true and returned an error when run on a primary if the value was false, because the command blocked all other operations.在v4.4之前,如果该值为true,则此布尔字段允许compact副本集中的primary上运行,如果值为false,则在primary上执行时返回错误,因为该命令阻止了所有其他操作。

Starting in v4.4, compact does not block MongoDB CRUD Operations on the database it is compacting.从v4.4开始,compact不会阻止正在压缩的数据库上的MongoDB CRUD操作

commentany

Optional. 可选。A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:用户提供了附加到此命令的注释。设置后,此注释将与此命令的记录一起显示在以下位置:

A comment can be any valid BSON type(string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

New in version 4.4.在版本4.4中新增

Warning警告

Always have an up-to-date backup before performing server maintenance such as the compact operation.在执行服务器维护(如compact操作)之前,请始终保持最新的备份。

compact Required Privileges所需权限

For clusters enforcing authentication, you must authenticate as a user with the compact privilege action on the target collection. 对于强制执行身份验证的集群,您必须以用户身份对目标集合执行compact权限操作。The dbAdmin role provides the required privileges for running compact against non-system collections.dbAdmin角色提供对非系统集合运行compact所需的权限。

For system collections, create a custom role that grants the compact action on the system collection. 对于系统集合,创建一个自定义角色,授予系统集合上的compact操作。You can then grant that role to a new or existing user and authenticate as that user to perform the compact command. 然后,您可以将该角色授予新用户或现有用户,并作为该用户进行身份验证以执行compact命令。For example, the following operations create a custom role that grants the compact action against specified database and collection:例如,以下操作创建一个自定义角色,该角色授予对指定数据库和集合的compact操作:

use admin
db.createRole(
   {
      role: "myCustomCompactRole",
      privileges: [
         {
            resource: { "db" : "<database>" , "collection" : "<collection>" },
            actions: [ "compact" ]
         }
      ],
      roles: []
   }
)

For more information on configuring the resource document, see Resource Document.有关配置resource文档的更多信息,请参阅资源文档

To add the dbAdmin or the custom role to an existing user, use db.grantRolesToUser() or db.updateUser(). 要将dbAdmin或自定义角色添加到现有用户,请使用db.grantRolesToUser()db.updateUser()The following operation grants the custom compact role to the myCompactUser on the admin database:以下操作将自定义compact角色授予admin数据库上的myCompactUser

use admin
db.grantRolesToUser("myCompactUser", [ "dbAdmin" | "myCustomCompactRole" ] )

To add the dbAdmin or the custom role to a new user, specify the role to the roles array of the db.createUser() method when creating the user.要将dbAdmin或自定义角色添加到新用户,请在创建用户时将角色指定到db.createUser()方法的roles数组中。

use admin
db.createUser(
  {
     user: "myCompactUser",
     pwd: "myCompactUserPassword",
     roles: [
       { role: "dbAdmin", db: "<database>" } | "myCustomCompactRole"
     ]
   }
)

Behavior行为

Blocking舞台调度

Changed in version 4.4.在版本4.4中更改

Starting in v4.4, on WiredTiger, compact only blocks the following metadata operations:从v4.4开始,在WiredTiger上,compact仅阻止以下元数据操作:

compact does not block MongoDB CRUD Operations for the database it is currently operating on.不会为当前正在运行的数据库阻止MongoDB CRUD操作

Before v4.4, compact blocked all operations for the database it was compacting, including MongoDB CRUD Operations, and was therefore recommended for use only during scheduled maintenance periods. 在v4.4之前,compact阻止了正在压缩的数据库的所有操作,包括MongoDB CRUD操作,因此建议仅在计划维护期间使用。Starting in v4.4, the compact command is appropriate for use at any time.从v4.4开始,compact命令适合在任何时候使用。

You may view the intermediate progress either by viewing the mongod log file or by running the db.currentOp() in another shell instance.您可以通过查看mongod日志文件或在另一个shell实例中运行db.currentOp()来查看中间进度。

Operation Termination操作终止

If you terminate the operation with the db.killOp() method or restart the server before the compact operation has finished, be aware of the following:如果使用db.killOp()方法终止操作或在compact操作完成之前重新启动服务器,请注意以下事项:

  • If you have journaling enabled, the data remains valid and usable, regardless of the state of the compact operation. 如果启用了日志记录,则无论compact操作的状态如何,数据都保持有效和可用。You may have to manually rebuild the indexes.您可能必须手动重建索引。
  • If you do not have journaling enabled and the mongod or compact terminates during the operation, it is impossible to guarantee that the data is in a valid state.如果没有启用日志记录,并且mongodcompact在操作期间终止,则无法保证数据处于有效状态。
  • In either case, much of the existing free space in the collection may become un-reusable. 在这两种情况下,集合中的大部分现有可用空间都可能变得不可重用。In this scenario, you should rerun the compaction to completion to restore the use of this free space.在这种情况下,您应该重新运行压缩到完成,以恢复此可用空间的使用。

Disk Space磁盘空间

To see how the storage space changes for the collection, run the collStats command before and after compaction.要查看集合的存储空间如何变化,请在压缩前后运行collStats命令。

On WiredTiger, compact attempts to reduce the required storage space for data and indexes in a collection, releasing unneeded disk space to the operating system. WiredTiger上,compact试图减少集合中数据和索引所需的存储空间,从而将不需要的磁盘空间释放给操作系统。The effectiveness of this operation is workload dependent and no disk space may be recovered. 此操作的有效性取决于工作负载,无法恢复任何磁盘空间。This command is useful if you have removed a large amount of data from the collection, and do not plan to replace it.如果您从集合中删除了大量数据,并且不打算替换它,则此命令非常有用。

compact may require additional disk space to run on WiredTiger databases.可能需要额外的磁盘空间才能在WiredTiger数据库上运行。

Replica Sets复制集

compact commands do not replicate to secondaries in a replica set.命令不会复制到副本集中的辅助副本。

  • Compact each member separately.分别压实每个构件。
  • Ideally run compact on a secondary. 理想情况下,在辅助设备上运行compactSee option force above for information regarding compacting the primary.有关压缩primary的信息,请参阅上面的选项force
  • On secondaries, the compact command forces the secondary to enter RECOVERING state. 在secondary上,compact命令强制辅助设备进入RECOVERING状态。Read operations issued to an instance in the RECOVERING state will fail. 向处于RECOVERING状态的实例发出的读取操作将失败。This prevents clients from reading during the operation. When the operation completes, the secondary returns to SECONDARY state.这将防止客户端在操作期间进行读取。操作完成后,辅助设备将返回SECONDARY状态。

    See Replica Set Member States for more information about replica set member states.有关副本集成员状态的详细信息,请参阅副本集成员状态

See Perform Maintenance on Replica Set Members for an example replica set maintenance procedure to maximize availability during maintenance operations.请参阅对副本集成员执行维护,以了解在维护操作期间最大化可用性的示例副本集维护过程。

Sharded Clusters分片状群集

compact only applies to mongod instances. 仅适用于mongod实例。In a sharded environment, run compact on each shard separately as a maintenance operation.在分片环境中,作为维护操作,分别在每个分片上运行compact

You cannot issue compact against a mongos instance.不能针对mongos实例发布compact

Capped Collections封顶集合

On WiredTiger, the compact command will attempt to compact the collection.WiredTiger上,compact命令将尝试压缩集合。

Index Building索引生成

mongod rebuilds all indexes in parallel following the compact operation.compact操作之后并行重建所有索引。

←  collModconnPoolSync →