1. 前言
mongodb文檔中存儲一節中關於磁盤空間回收的說明,簡單處理、備注記於此。標號為另加,原文檔見:https://docs.mongodb.com/v3.2/faq/storage/
2. Why are the files in my data directory larger than the data in my database?
2.1 ...(省略3節,關於預分配空間、oplog、journal)
2.2 Empty records
MongoDB maintains lists of empty records in data files as it deletes documents and collections. MongoDB can reuse this space, but will not, by default, return this space to the operating system.
To allow MongoDB to more effectively reuse the space, you can de-fragment your data. To de-fragment, use the compact command. The compact requires up to 2 gigabytes of extra disk space to run. Do not use compact if you are critically low on disk space. For more information on its behavior and other considerations, see compact.
compact only removes fragmentation from MongoDB data files within a collection and does not return any disk space to the operating system. To return disk space to the operating system, see How do I reclaim disk space?.
2.3 按
上文提到可通過compact命令處理空記錄導致的fragmentation,說這並不會向OS返還磁盤空間;但查詢文檔發現如果采用的是WiredTriger存儲引擎,這部分磁盤空間是被返還給OS的;另外compact的用法是:
db.yourCollection.runCommand("compact");
或
db.runCommand({ compact : 'yourCollection' });
3. How do I reclaim disk space?
The following provides some options to consider when reclaiming disk space.
NOTE
You do not need to reclaim disk space for MongoDB to reuse freed space. See Empty records for information on reuse of freed space.
3.1 repairDatabase
You can use repairDatabase on a database to rebuilds the database, de-fragmenting the associated storage in the process.
repairDatabase requires free disk space equal to the size of your current data set plus 2 gigabytes. If the volume that holds dbpath lacks sufficient space, you can mount a separate volume and use that for the repair. For additional information and considerations, see repairDatabase.
WARNING
Do not use repairDatabase if you are critically low on disk space.
repairDatabase will block all other operations and may take a long time to complete.
You can only run repairDatabase on a standalone mongod instance.
You can also run the repairDatabase operation for all databases on the server by restarting your mongod standalone instance with the --repair and --repairpath options. All databases on the server will be unavailable during this operation.
3.2 按
運行compact命令需要至少2G的磁盤空間,repairDatabase需要的磁盤空間則與數據庫大小有關,若數據庫為x,則需要至少預留x+2。