
What Is an Index in MongoDB? Beginner Guide with Examples
- Introduction to MongoDB
- Installation & Database Creation
- CRUD Operations
- Embedded Documents and Arrays
- Validation Rules - Enforcing Structure in MongoDB
- Visualize MongoDB Relationships (Embedded vs Referenced)
- What Is an Index in MongoDB? (You are here).
As your MongoDB collections grow, your queries may start to slow down. This chapter explains how indexes can help improve query performance by allowing MongoDB to quickly locate documents without scanning the entire collection.
This guide is part of the StreamFlix series and uses examples from our movie, user, and review collections.
What Is an Index in MongoDB
An index in MongoDB is similar to an index at the back of a book. Instead of flipping through every page, you can go directly to the content you need. Indexes allow MongoDB to quickly find the data without scanning every document in a collection.
Without indexes, MongoDB performs a full collection scan. With indexes, it can jump directly to the matching data.
Why Indexes are Important
- Improve query performance, especially on large collections
- Reduce CPU and memory usage during queries
- Support efficient sorting and filtering
- Essential for scalable applications
In our StreamFlix example, imagine a reviews
collection with millions of documents. If you want to find all reviews by one user, an index on user_id
can make that query much faster.
Method I. Types of Indexes in MongoDB Shell
Single-field Index
This is the most basic index. It is created on a single field.
Example: Index on user_id
in the reviews
collection.
|
This makes queries like the following much faster:
|
Compound Index
Created on multiple fields. Useful for queries that filter or sort by more than one field.
Example: Index on { movie_id, rating }
in the reviews
collection.
|
When creating an index in MongoDB, you can specify the sort order for each field in the index:
1
means ascending order-1
means descending order
In most cases, MongoDB will use the index efficiently regardless of whether it’s ascending or descending, but setting the correct order can help when your queries also use sort()
.
This compound index means:
First, MongoDB will sort or match by movie_id in ascending order.
Then, for documents with the same movie_id, it will sort by rating in descending order.
This is useful if you want to find all reviews for a movie and show the highest ratings first:
|
Text Index
Used for full-text search on string fields.
Example: Index on title
and description
in the movies
collection.
|
Search with:
|
$text
is a MongoDB query operator used to search in fields covered by a text index.$search
specifies the string or phrase you are looking for.
In this example, MongoDB will look for the word "superhero"
in both the title
and description
fields, using the text index to find matches efficiently.
This works only if a text index is already created on those fields.
Mehtod II. Creating Indexes Visually in DbSchema
If you’re using DbSchema, you can create indexes without writing any code.
Steps
- Open your MongoDB model in DbSchema.
- Select the collection, for example
reviews
. - Double-click to open the structure editor.
- Go to the Indexes tab.
- Click “Add Index”.
- Select one or more fields, such as
user_id
. - Save the index.
DbSchema will generate the equivalent MongoDB shell script for the index. You can apply the index directly from the interface or export the script to run manually.
Conclusion
Indexes are a critical part of working with MongoDB efficiently. They speed up queries, reduce server load, and are essential for scaling your application.
What’s Next: Aggregation Pipelines in MongoDB
In the next chapter, we’ll dive into aggregation pipelines, one of the most powerful features in MongoDB.
An aggregation pipeline allows you to process data step by step, similar to how data flows through a production line. You can filter, group, sort, calculate averages, reshape documents, and much more - all within the database.
Instead of writing complex logic in your application code, you can define it using MongoDB’s built-in stages like $match
, $group
, $project
, $sort
, and others.
We’ll continue using the StreamFlix example to show how to:
- Find the most reviewed movies
- Calculate average ratings per movie
- Count how many users posted reviews per month
- Use aggregation visually in DbSchema
Aggregation pipelines are essential for generating reports, building analytics dashboards, or cleaning up your data - all directly inside MongoDB.