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.
Related videos on Youtube
Comments
-
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 about 6 yearsThis is really helpful, but I think that
printjson(indexes);
should beprintjson(idx);
-
Filomat over 2 yearsit works perfect