List Indexes | MongoDB Indexing Tutorial

Compound Index | MongoDB Indexing Tutorial

Index Performance | MongoDB Indexing Tutorial

Indexing Types | MongoDB Indexing Tutorial

Unique Index | MongoDB Indexing Tutorial

List Indexes | MongoDB Indexing Tutorial

Create Index | MongoDB Indexing Tutorial

List indexes in a collection

db.getCollection('documents').getIndexes()

This returns all of the indexes on a people collection.

This operation returns an array of objects representing the different indexes...

[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_"
    },
    {
        "v" : 2,
        "key" : {
            "email" : 1.0,
            "firstName": 1.0
        },
        "name" : "email_1"
    }
]

Notice how the array returns two objects. This is because we've defined two indexes on our people collection.

v indicates the version of the index.

key indicates the fields(s) used in the index. Notice how the first entry has the single field key _id whereas the second represents a compound index ({email:1, firstName:1})

name represents the name given to the index.

List indexes in entire DB

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

This loops through all of the collections in one go to generate the same output for each index in one go...

Indexes for people:
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_"
	},
	{
		"v" : 2,
		"key" : {
			"email" : 1,
			"firstName" : 1
		},
		"name" : "email_1"
	}
]
Indexes for places:
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_"
	},
	{
		"v" : 2,
		"key" : {
			"location" : 1
		},
		"name" : "location_1"
	}
]

Notice how we can list all of the indexes in one operation. We get both the indexes for the people and places collection in one go.

Your thoughts?