Last active
February 5, 2021 11:22
-
-
Save karan9/2f5847a47dbc99942fd95624c1932b68 to your computer and use it in GitHub Desktop.
A simple Mongo Shell function to get dataSize, storageSize and indexSize for each DB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
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 || 0; | |
totalStorageSize += db.stats(formatSize).storageSize || 0; | |
totalIndexSize += db.stats(formatSize).indexSize || 0; | |
}); | |
// Show total information | |
print("\nTotal:"); | |
print(`dataSize: ${totalDataSize}`); | |
print(`storageSize: ${totalStorageSize}`); | |
print(`indexSize: ${totalIndexSize}`); | |
} | |
getStorageDetails(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
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