Mongodb v3.4 – wiredtiger – compact does not work

mongodbwiredtiger

I am using mongo v3.4.10 with WiredTiger. I run the following command:

db.runCommand({ compact: "my_collection" });

the response is:

/* 1 */
{
    "ok" : 1.0
}

and the mongo log shows:

2017-10-31T00:12:20.282+0000 I COMMAND  [conn23] compact mydb.my_collection begin, options: paddingMode: NONE validateDocuments: 1
2017-10-31T00:12:21.662+0000 I COMMAND  [conn23] compact mydb.my_collection end
2017-10-31T00:12:21.663+0000 I COMMAND  [conn23] command mydb.my_collection appName: "MongoDB Shell" command: compact { compact: "my_collection" } numYields:0 reslen:22 locks:{ Global: { acquireCount: { r: 1, w: 1 } }, Database: { acquireCount: { W: 1 } } } protocol:op_command 1381ms

In short, everything looks fine, but the storageSize is exactly the same :/

I deleted lots of fields with a value of null, so I would expect a significant drop in disk usage.

I read online that early v3 had issues with this, but I'm on the latest version right now. The database is quite large so if I can avoid repairDatabase that would be good.

Any help is appreciated, thx.


UPDATE: I performed a mongodump / mongorestore, and it resulted in a 9% disk use reduction (admittedly I was expecting more). Nonetheless, it seems like compact should have had an impact.

UPDATE 2: I performed a repairDatabase and it had no effect. This is explicitly stated in mongo docs (https://docs.mongodb.com/manual/reference/command/repairDatabase):

For WiredTiger, the operation rebuilds the database but does not result in the compaction of the collections in the database

but i thought i'd give it a crack. Still not sure why compact did nothing though.

Best Answer

@mils, The compact administration commands Rewrites and defragments all data and indexes in a collection. On WiredTiger databases, this command will release unneeded disk space to the operating system.

compact has the following form:

{ compact: <collection name> }

In you case after the db.runCommand(command) , you have got the message such as

{
    "ok" : 1.0
}

it means your command has successfully executed.

On WiredTiger, compact will rewrite the collection and indexes to minimize disk space by releasing unused disk space to the system. This is useful if you have removed a large amount of data from the collection, and do not plan to replace it.

I am also attaching the db.runCommand(command) picture from mongo shell command,for proper format understanding of that command.

enter image description here

WARNING:- Always have an up-to-date backup before performing server maintenance such as the compact operation.

How to check the storage engine from the mongo command shell

db.serverStatus().storageEngine

Note : - As per MongoDB BOL the WiredTiger Storage Engine From the MongoDB version 3.2: The WiredTiger storage engine is the default storage engine starting in MongoDB 3.2. For existing deployments, if you do not specify the --storageEngine or the storage.engine setting, MongoDB 3.2 can automatically determine the storage engine used to create the data files in the --dbpath or storage.dbPath.

For further your ref compact