What does Mongodb's compact actually do?

1.9k views Asked by At

Well, probably this should be clear from the documentation, sadly, this is not the case. My guess was, that compact make some kind of defragmentation of the colection - such that collection's paddingfactor would go bellow given value. However, this does not seem to be true:

> db.aaa.stats().paddingFactor
1.9980000000000002
> db.runCommand ( { compact: 'aaa', paddingFactor: 1.1 } )
{ "ok" : 1 }
> db.aaa.stats().paddingFactor
1.9980000000000002
> db.runCommand ( { compact: 'aaa', paddingFactor: 3 } )
{ "ok" : 1 }
> db.aaa.stats().paddingFactor
1.9980000000000002

If this is not the way to change the padding factor of a collection, is there some other way to do so?

1

There are 1 answers

1
BatScream On BEST ANSWER

My guess was, that compact make some kind of DE-fragmentation of the collection

Yes. It rewrites, DE-fragments all the data and the indexes on the collection.

such that collection's paddingfactor

No. It does not change the padding factor of the collection.

From the docs:

compact compacts existing documents but does not reset paddingFactor statistics for the collection. After the compact MongoDB will use the existing paddingFactor when allocating new records for documents in this collection.

So when you execute the below command,

db.runCommand ( { compact: 'aaa', paddingFactor: 1.1 } )

the record size of the existing documents in the collection get altered using the specified padding factor. That is, all the existing records will now have 10% of their document size as their padding size.

So after the DE-fragmentation all the existing records will have their sizes altered as,

record size = document size + (10% of document size)

And it does not the affect or change padding factor for the entire collection.

way to change the padding factor of a collection, is there some other way to do so?

No. You can't select nor change the padding factor for a collection. From the docs:

padding factor: An automatically-calibrated constant used to determine how much extra space MongoDB should allocate per document container on disk. A padding factor of 1 means that MongoDB will allocate only the amount of space needed for the document. A padding factor of 2 means that MongoDB will allocate twice the amount of space required by the document.