Getting the Size of a Specific Index in MongoDB

Spent a little while trying to find the size of a specific index this morning and couldn't find any documentation on how to do it. Eventually stumbled on it in db.collection_name.stats()

> db.content.stats()
{
    "ns" : "conversocial.content",
    "sharded" : false,
    "primary" : "main01",
    "ns" : "conversocial.content",
    "count" : 1924859,
    "size" : 1578724996,
    "avgObjSize" : 820.1769563381006,
    "storageSize" : 1746546688,
    "numExtents" : 23,
    "nindexes" : 2,
    "lastExtentSize" : 301682688,
    "paddingFactor" : 1,
    "flags" : 0,
    "totalIndexSize" : 179773888,
    "indexSizes" : {
        "_id_" : 56226352,
        "source_1_puid_1" : 123547536
    },
    "ok" : 1
}

Also, db.collection_name.totalIndexSize(true) will list these:

> db.content.totalIndexSize(true)
_id_   56226352
source_1_puid_1 123547536
179773888

And an easy script to get the indices in mb:

s = db.content.stats()['indexSizes']; 
for (key in s) { 
    print(key + ': ' + s[key] / (1024 * 1024))
}

Discussion

blog comments powered by Disqus

Colin Howe

I'm Colin. I like coding, ultimate frisbee and startups. I am VP of engineering at Conversocial