Skip to content

Instantly share code, notes, and snippets.

@karan9
Created October 21, 2020 09:23
Show Gist options
  • Save karan9/420bb4a8f856f540fe54d980f25b7c9c to your computer and use it in GitHub Desktop.
Save karan9/420bb4a8f856f540fe54d980f25b7c9c 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 });
// Loop over and get storage stats for each database
dbs.forEach(function(database) {
db = db.getSiblingDB(database.name);
print("\nDB: " + database.name);
print("dataSize: " + db.stats(formatSize).dataSize);
print("storageSize: " + db.stats(formatSize).storageSize);
print("indexSize: " + db.stats(formatSize).indexSize);
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