Mongodb – if compact is required after move chunk

mongodb

I have mongodb (wired tiger) with two shards.
I added third shard and enabled the balancer.

I can see the logs where the data chunks are getting moved to the new shard.
After the movement of chunk where the disk space of old shards( from which data is moved) will be returned to the OS or compact command has to be executed.

Best Answer

As at MongoDB 4.0, chunk migration for a sharded collection does not automatically run compact or free disk space when complete. However, storage used by documents that have been migrated is marked as available for reuse for new data insertion/updates. An underlying assumption is that you will continue to add data to shards over time, so disk space does not need to be aggressively freed.

If you do want to free up disk space after significant changes in storage usage (for example, removing or migrating a large number of documents) you can manually compact collections.

It is important to note that the compact command currently blocks operations for the database being compacted, so is not advisible to run on a production system without careful consideration of impact. One approach would be to compact each of your original shards using rolling maintenance (compacting one secondary at a time). If you have a significant number of collections to compact, you could also re-sync secondaries (one at at time) to rebuild all data files without downtime.