db.stats()
and db.$collection_name.stats()
to analyze disk space usage. For detailed information, see the following table.Analysis Command | Command Definition |
This command is used to retrieve the statistical information of the current database. Executing the db.stats() command returns a document containing various information about the current database. For example: database name, data size, index size, number of collections, etc. | |
This command is used to retrieve the statistical information of a specified collection. Executing the db.collection.stats() command returns a document containing various information about the specified collection. For example: collection name, number of documents, data size, number of indexes, etc. | |
This command is used to retrieve the storage space size occupied by a specified collection. Executing the db.collection.storageSize() command returns the storage space size occupied by the specified collection, in bytes. The storage space size returned by this command includes the space occupied by data and indexes in the collection, but does not include other overheads of the MongoDB instance, such as log files and temporary files. | |
This command is used to retrieve the storage space size occupied by all indexes of a specified collection. Executing the db.collection.totalIndexSize() command returns the storage space size occupied by all indexes of the specified collection, in bytes. The storage space size returned by this command does not include the space occupied by data in the collection, only the space occupied by all indexes of the collection. | |
This command is used to retrieve the total storage space size occupied by a specified collection. Executing the db.collection.totalSize() command returns the total storage space size occupied by the specified collection, in bytes. The storage space size returned by this command includes the space occupied by data and indexes in the collection, as well as other overheads of the MongoDB instance, such as log files and temporary files. |
## Creating a Function for Querying the Fragmentation Ratesfunction getCollectionDiskSpaceFragRatio(dbname, coll) { var res = db.getSiblingDB(dbname).runCommand({ collStats: coll }); var totalStorageUnusedSize = 0; var totalStorageSize = res['storageSize'] + res['totalIndexSize']; Object.keys(res.indexDetails).forEach(function(key) { var size = res['indexDetails'][key]['block-manager']['file bytes available for reuse']; print("index table " + key + " unused size: " + size); totalStorageUnusedSize += size; }); var size = res['wiredTiger']['block-manager']['file bytes available for reuse']; print("collection table " + coll + " unused size: " + size); totalStorageUnusedSize += size; print("collection and index table total unused size: " + totalStorageUnusedSize); print("collection and index table total file size: " + totalStorageSize); print("Fragmentation ratio: " + ((totalStorageUnusedSize * 100.0) / totalStorageSize).toFixed(2) + "%"); }## Specifying a Database for Querying the Fragmentation Rates of All Collections use xxxdb db.getCollectionNames().forEach((c) => {print("\\n\\n" + c); getCollectionDiskSpaceFragRatio(db.getName(), c);});
db.runCommand({compact:"collectionName"})
to compress documents within the specified collection, thereby releasing disk space. In this command, replace collectionName
with the actual name of the targeted collection.compact
operation. This is because doing so will block all incoming requests to the instance and might potentially result in issues where the Compact process does not effectively manage indexes. The solutions are as follows:campact
operation, as MongoDB inherently reuses this space. In such circumstances, consider expanding the disk space. For specific operations, see Change Mongod Node Configuration Specifications.
Was this page helpful?