A list of indices in MongoDB?

35,416

Solution 1

From the shell:

db.test.getIndexes()

For shell help you should try:

help;
db.help();
db.test.help();

Solution 2

If you want to list all indexes across collections:

db.getCollectionNames().forEach(function(collection) {
   indexes = db.getCollection(collection).getIndexes();
   print("Indexes for " + collection + ":");
   printjson(indexes);
});

Solution 3

And if you want to get list of all indexes in your database:

use "yourdbname"

db.system.indexes.find()

Solution 4

Make sure you use your collection:

db.collection.getIndexes()

http://docs.mongodb.org/manual/administration/indexes/#information-about-indexes

Solution 5

You can also output all your indexes together with their size:

db.collectionName.stats().indexSizes

Also check that db.collectionName.stats() gives you a lot of interesting information like paddingFactor, size of the collection and number of elements inside of it.

Share:
35,416

Related videos on Youtube

Timmy
Author by

Timmy

timmy.

Updated on July 08, 2022

Comments

  • Timmy
    Timmy almost 2 years

    Is there a way to see a list of indices on a collection in mongodb in shell? i read through http://www.mongodb.org/display/DOCS/Indexes but i dont see anything

  • Curt Williams
    Curt Williams about 6 years
    This is really helpful, but I think that printjson(indexes); should be printjson(idx);
  • Filomat
    Filomat over 2 years
    it works perfect