

db.stats() and db.$collection_name.stats() to analyze disk space usage. For detailed information, please see the table below.Analysis command | Command meaning |
This command is used to access the statistical information of the current database. Executing the db.stats() command returns a document containing various information about the current database, such as: database name, data size, index size, number of collections, etc. | |
This command is used to access the statistical information of a specified collection. Executing the db.collection.stats() command returns a document containing various information about the specified collection, such as: collection name, number of documents, data size, number of indexes, etc. | |
This command is used to access 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 access 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 access 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. |
## Function to generate peek fragmentation ratefunction 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) + "%"); }## Specify a database to peek at 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 compact specified collection documents and release disk space. Here, collectionName is the name of the collection, replace it according to the actual situation.compact as it will block all requests to the instance and may lead to issues where the Compact index is invalid. The solutions are as follows:compact operation is not recommended, as MongoDB will reuse this space. In this case, please expand the disk space. For specific operations, please see Change Mongod node configuration specifications.Esta página foi útil?
Você também pode entrar em contato com a Equipe de vendas ou Enviar um tíquete em caso de ajuda.
comentários