Skip to content

Instantly share code, notes, and snippets.

@karan9
Last active November 10, 2020 14:25
Show Gist options
  • Save karan9/d5e5d364c185f4095b2053ddcde73331 to your computer and use it in GitHub Desktop.
Save karan9/d5e5d364c185f4095b2053ddcde73331 to your computer and use it in GitHub Desktop.
function getStorageDetails() {
var db = this.db.getSiblingDB("admin");
var totalIndexSize = 0,
totalStorageSize = 0,
totalDataSize = 0,
formatSize = 1024 * 1024 * 1024;
// List the databases
var dbs = db.runCommand({ listDatabases: 1 }).databases;
// Loop over and get storage stats for each database
dbs.forEach(function(database) {
db = db.getSiblingDB(database.name);
coll = db.getCollectionNames();
print(`\nDB: ${database.name}:`);
print(`dataSize: ${db.stats(formatSize).dataSize}`);
print(`storageSize: ${db.stats(formatSize).storageSize}`);
print(`indexSize: ${db.stats(formatSize).indexSize}`);
// Loop over each collection and print the totalIndexSize
coll.forEach(function(collection) {
print(`\nCollection: ${collection}:`);
print(`indexSize: ${db.getCollection(collection).stats(formatSize).totalIndexSize}`);
print(`dataSize: ${db.getCollection(collection).stats(formatSize).dataSize}`);
print(`storageSize: ${db.getCollection(collection).stats(formatSize).storageSize}`);
});
print(`\n=====================================`);
totalDataSize += db.stats(formatSize).dataSize;
totalStorageSize += db.stats(formatSize).storageSize;
totalIndexSize += db.stats(formatSize).indexSize;
});
// Show total information
print("\nTotal:");
print(`dataSize: ${totalDataSize}`);
print(`storageSize: ${totalStorageSize}`);
print(`indexSize: ${totalIndexSize}`);
}
getStorageDetails();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment