Here is a MongoDB script for presenting index information in a more concise way than getIndexes() provides. This script also presents an index’s total size along with a breakdown of its size on all of the shards.
//mongo --eval="var collection='file';"
var ret = db[collection].getIndexes().map(function(i){
return {"key":i.key, "name":i.name};
});
var o = {};
for(r in ret) {
o[ret[r].name] = ret[r].key;
}
var cstats = db[collection].stats();
for(k in cstats.indexSizes) {
o[k].totalsize = cstats.indexSizes[k];
}
var shardinfo = cstats.shards;
for(s in shardinfo) {
for(k in shardinfo[s].indexSizes) {
if(!o[k].shards) o[k].shards = {};
o[k].shards[s] = shardinfo[s].indexSizes[k];
}
}
printjson(o);
Produces the following output:
