On this page本页内容
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:采用以下字段:
compact | string | |
force | flag |
|
comment | any |
|
compact
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" ] } )
Changed in version 4.4.在版本4.4中更改。
Starting in v4.4, on WiredTiger, 从v4.4开始,在WiredTiger上,compact
only blocks the following metadata operations:compact
仅阻止以下元数据操作:
db.collection.drop()
db.collection.createIndex()
db.collection.createIndexes()
db.collection.dropIndex()
db.collection.dropIndexes()
compact
does not block MongoDB CRUD Operations for the database it is currently operating on.不会为当前正在运行的数据库阻止MongoDB CRUD操作。
Before v4.4, 在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. compact
阻止了正在压缩的数据库的所有操作,包括MongoDB CRUD操作,因此建议仅在计划维护期间使用。Starting in v4.4, the 从v4.4开始,compact
command is appropriate for use at any time.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()
来查看中间进度。
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
操作完成之前重新启动服务器,请注意以下事项:
compact
operation. compact
操作的状态如何,数据都保持有效和可用。mongod
or compact
terminates during the operation, it is impossible to guarantee that the data is in a valid state.mongod
或compact
在操作期间终止,则无法保证数据处于有效状态。To see how the storage space changes for the collection, run the 要查看集合的存储空间如何变化,请在压缩前后运行collStats
command before and after compaction.collStats
命令。
On WiredTiger, 在WiredTiger上,compact
attempts to reduce the required storage space for data and indexes in a collection, releasing unneeded disk space to the operating system. 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数据库上运行。
compact
commands do not replicate to secondaries in a replica set.命令不会复制到副本集中的辅助副本。
compact
on a secondary. compact
。force
above for information regarding compacting the primary.force
。On secondaries, the 在secondary上,compact
command forces the secondary to enter RECOVERING
state. 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.请参阅对副本集成员执行维护,以了解在维护操作期间最大化可用性的示例副本集维护过程。
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
。
On WiredTiger, the 在WiredTiger上,compact
command will attempt to compact the collection.compact
命令将尝试压缩集合。
mongod
rebuilds all indexes in parallel following the 在compact
operation.compact
操作之后并行重建所有索引。