How To: List all indexes in MongoDB

Indexes are a very important structure in any database and the same is true with MongoDB as well. It adds spices to the performance of your query. It is also very important to know what and how many indexes are created on your DB.

In the normal life of DBA, one needs to find out all indexes created on a collection so that one can plan a new index or remove any unused index.
MongoDB does provide a method to retrieve the list of indexes on a collection.

Function getIndexes() will retrieve the list of all indexes of a collection

I have created a small DB for blogging purpose. Listing indexes of the collection named subreddits_40:

MongoDB Enterprise > var exp40=db.subreddits_40
MongoDB Enterprise >
MongoDB Enterprise >
MongoDB Enterprise > exp40.getIndexes()

“v” : 2,
“key” : {
id” : 1 }, “name” : “_id“,
“ns” : “subreddits_40.subreddits_40”
MongoDB Enterprise >

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.