Skip to content

Instantly share code, notes, and snippets.

@lsloan
Last active June 19, 2017 19:44
Show Gist options
  • Save lsloan/56eb33c33fd00e0e5f8602e934b318f1 to your computer and use it in GitHub Desktop.
Save lsloan/56eb33c33fd00e0e5f8602e934b318f1 to your computer and use it in GitHub Desktop.
JS: Display the number of bytes used by the indices of each collection in a MongoDB database.

From: https://stackoverflow.com/questions/7851563#37232484

A short JavaScript program (and a one-line version) to display the number of bytes used by the indices of each collection in a MongoDB database.

TODO: Use functions like each() to loop over the arrays, making it execute faster and the code shorter.

var indexesArr = {};
db.getCollectionNames().forEach(function(collection) {
indexes = db[collection].stats().indexSizes;
for (i in indexes) indexesArr[collection + " - " + i] = indexes[i];
});
var sortable = [], x;
for (x in indexesArr) sortable.push([x, indexesArr[x]]);
var pArr = sortable.sort(function(a, b) {return b[1] - a[1]});
for (x in pArr) print(pArr[x][1] + ": " + pArr[x][0]);
var indexesArr = {}; db.getCollectionNames().forEach(function(collection) {indexes = db[collection].stats().indexSizes; for (i in indexes) indexesArr[collection + " - " + i] = indexes[i]; }); var sortable = [], x; for (x in indexesArr) sortable.push([x, indexesArr[x]]); var pArr = sortable.sort(function(a, b) {return b[1] - a[1]}); for (x in pArr) print( pArr[x][1] + ": " + pArr[x][0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment