How do I check if an index is being used

后端 未结 2 1083
梦如初夏
梦如初夏 2020-12-08 20:10

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

2条回答
  •  自闭症患者
    2020-12-08 20:50

    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.

提交回复
热议问题