I have a mongodb replica set with a lot of databases, collections & indexes.
We did a lot of refactor and optimization and, of course, I have a lot of \"creative
The simplest solution to this is to use the mongodb inbuilt $indexStats
Using the Mongo console run -
db.collection.aggregate([ { $indexStats: { } } ])
Using PyMongo -
from pymongo import MongoClient
collection = MongoClient()[db_name][collection_name]
index_stats = collection.aggregate([{'$indexStats':{}}])
for index_info in index_stats:
print index_info
Apologies for re-opening an old question. This shows up on the first page of google searches and the only answer is to use a snippet of unmaintained code.
There is a pretty cool script out on Github that you should look at:
https://github.com/wfreeman/indexalizer
Basically it involves turning on profiling for your database and then it will use the data collected by the profiler to drive explain() calls. It then tells you both which indexes are not being used and which queries are not using indexes. Pretty slick.
More about mongoDB database profiling:
http://docs.mongodb.org/manual/reference/database-profiler/