Visit complete MongoDB roadmap

← Back to Topics List

Creating Indexes

Indexes are a powerful feature in MongoDB that help improve the performance of read operations (queries) in your database. They work similarly to the indexes found in a book, where you can quickly locate specific information rather than scanning through the entire content. In this section, we will discuss the basics of MongoDB indexes and their usage.

Overview of Indexes

Basically, an index in MongoDB is a data structure that holds a smaller version of the data in our documents, along with a reference to the original document. This smaller version is stored in an efficient manner, making it easier and faster to locate specific documents based on the indexed field(s).

Indexes can be created on one or more fields in a MongoDB collection. The default index that exists in every collection is the _id index, which ensures unique values for the _id field.

Types of Indexes

There are several types of indexes in MongoDB, including:

  • Single Field Index: Index based on a single field in the documents.
  • Compound Index: Index based on multiple fields in the documents.
  • Multikey Index: Index used when the indexed field contains an array of values.
  • Text Index: Index used to support text search queries on string content.
  • 2dsphere Index: Index used to support geospatial queries on spherical data.
  • 2d Index: Index used to support geospatial queries on planar data.

It’s important to choose the right type of index for the queries you will be running on your MongoDB collection.

Creating Indexes

To create an index on a field or fields, you can use the createIndex() method. Here’s an example of creating an index on the “username” field in the “users” collection:

db.users.createIndex({ username: 1 });

The 1 indicates that the index uses ascending order on the “username” field. You can also create a descending order index using -1 as the value.

For compound indexes, you can specify multiple fields like this:

db.users.createIndex({ username: 1, email: 1 });

Using Indexes

Once you have created an index on a field or fields, MongoDB will automatically use the appropriate index when you perform queries on the collection, optimizing the query execution.

To see which index is being used for a specific query, you can use the explain() method. For example, to see the index used for a query on the “username” field:

db.users.find({ username: 'John' }).explain();

This will give you detailed information about the query execution, including the index used.

Managing Indexes

To manage indexes, you can:

  • List all the indexes in a collection: db.COLLECTION_NAME.getIndexes().
  • Remove an index: db.COLLECTION_NAME.dropIndex(INDEX_NAME).
  • Remove all indexes: db.COLLECTION_NAME.dropIndexes().

Limitations and Considerations

While indexes are an amazing tool, they can have some caveats:

  • They consume storage space, so creating a large number of indexes may affect the storage capacity.
  • They can slow down write operations, as indexes should be updated whenever write operations occur on the indexed fields.
  • Indexes should be chosen wisely, considering the queries that will run on the collection.

In conclusion, MongoDB indexes are a vital aspect of optimizing query performance in your database. By understanding the different types of indexes and using them effectively, you can significantly improve the performance and efficiency of your MongoDB applications.

Community is the 6th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Roadmaps Best Practices Guides Videos Store YouTube by Kamran Ahmed

Community created roadmaps, articles, resources and journeys to help you choose your path and grow in your career.

© · FAQs · Terms · Privacy


The leading DevOps resource for Kubernetes, cloud-native computing, and the latest in at-scale development, deployment, and management.