Skip to content

Instantly share code, notes, and snippets.

@pulecp
Last active August 3, 2018 09:41
Show Gist options
  • Save pulecp/f1a7fbd68593d824d2c1161b037b67c5 to your computer and use it in GitHub Desktop.
Save pulecp/f1a7fbd68593d824d2c1161b037b67c5 to your computer and use it in GitHub Desktop.
MongoDB - show space available to reuse per collection
// mongo --quiet 127.0.0.1:27017/admin mongo_space_available_for_reuse.js
db.getMongo().setSlaveOk()
var dbs = db.runCommand({ "listDatabases": 1 }).databases
var total_available_space_to_reuse = 0
print("Space available to reuse per collection:\n")
dbs.forEach(function(database) {
db = db.getSiblingDB(database.name)
print(" " + db + ":")
var available_size = 0
db.getCollectionNames().forEach(function(col) {
var available_size_in_collection = db[col].stats().wiredTiger['block-manager']['file bytes available for reuse']
if (available_size_in_collection > 0) {
print(" " + col + ": " + available_size_in_collection / 1024 / 1024 + " MB")
}
available_size = available_size + available_size_in_collection
})
var available_size_mb = available_size / 1024 / 1024
total_available_space_to_reuse = total_available_space_to_reuse + available_size_mb
})
print("\n\ntotal available space to reuse: " + total_available_space_to_reuse + " MB")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment