DbSchema Database Designer

What Is an Index in MongoDB? Beginner Guide with Examples



Visualize MongoDB Relationships

  1. Introduction to MongoDB
  2. Installation & Database Creation
  3. CRUD Operations
  4. Embedded Documents and Arrays
  5. Validation Rules - Enforcing Structure in MongoDB
  6. Visualize MongoDB Relationships (Embedded vs Referenced)
  7. 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.

db.reviews.createIndex({ user_id: 1 })

This makes queries like the following much faster:

db.reviews.find({ user_id: ObjectId("...") })

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.

db.reviews.createIndex({ movie_id: 1, rating: -1 })

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:

db.reviews.find({ movie_id: ObjectId("...") }).sort({ rating: -1 })

Text Index

Used for full-text search on string fields.

Example: Index on title and description in the movies collection.

db.movies.createIndex({ title: "text", description: "text" })

Search with:

db.movies.find({ $text: { $search: "superhero" } })
  • $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.

Create Index in MongoDB

Steps

  1. Open your MongoDB model in DbSchema.
  2. Select the collection, for example reviews.
  3. Double-click to open the structure editor.
  4. Go to the Indexes tab.
  5. Click “Add Index”.
  6. Select one or more fields, such as user_id.
  7. 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.

Create Index in MongoDB

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.

Next Lesson

DbSchema Database Designer
Visual Design & Schema Diagram

➤ Create and manage your database schema visually through a user-friendly graphical interface.

➤ Easily arrange tables, columns, and foreign keys to simplify complex database structures, ensuring clarity and accessibility.

GIT & Collaboration
Version Control & Collaboration

➤ Manage schema changes through version control with built-in Git integration, ensuring every update is tracked and backed up.

➤ Collaborate efficiently with your team to maintain data integrity and streamline your workflow for accurate, consistent results.

Data Explorer & Query Builder
Relational Data & Query Builder

➤ Seamlessly navigate and visually explore your database, inspecting tables and their relationships.

➤ Build complex SQL queries using an intuitive drag-and-drop interface, providing instant results for quick, actionable insights.

Interactive Documentation & Reporting
HTML5 Documentation & Reporting

➤ Generate HTML5 documentation that provides an interactive view of your database schema.

➤ Include comments for columns, use tags for better organization, and create visually reports.