We help organizations cut through the noise, evaluate the right options, and move forward with greater clarity.
Whether you are replacing legacy systems or supporting growth, let’s define the right next step for your business.
Watch this on-demand webinar to learn how AI is reshaping FP&A for modern finance teams.
Come meet Delbridge in Austin, Texas, where Delbridge is sponsoring this year’s Vena Excelerate Conference!
Featuring a Real-World E-Commerce Case Study
An index in MongoDB is a data structure that significantly improves query performance by allowing efficient data lookups—similar to a book’s table of contents. Without indexes, MongoDB performs a full collection scan, which becomes inefficient as data grows.
Indexes map field values to document locations, reducing the number of documents MongoDB needs to scan. For example, searching for orders with status: “pending” without an index means scanning every document. With an index, MongoDB can jump directly to the relevant results.
MongoDB uses B-tree data structures to power its indexes. When a query runs, MongoDB consults the index to quickly locate matching documents, minimizing disk I/O and speeding up query execution.
Let’s explore a common scenario: an e-commerce platform with an orders collection containing millions of documents. Each document includes fields like customer_id, order_date, status, and total_amount.
A frequently run query looks like this:
Users report slow performance. Let’s diagnose and optimize this query using a step-by-step approach.
The following screenshot is from the Explain Plan view in Compass:

Use explain(“executionStats“) to inspect how MongoDB executes the query:
Sample Output:
Analysis:
The query is slow because it scans the entire collection due to the absence of an index on customer_id and status. This is inefficient for a collection with millions of documents.
Since the query filters on customer_id and status, create a compound index to cover both fields:
Why a Compound Index?
A compound index supports queries that filter on the indexed fields in the order they are defined. Here, it optimizes queries filtering on customer_id alone or both customer_id and status.
Run the query again with explain(“executionStats“):
Analysis:
Remove Unused Indexes: Drop indexes not used by queries to save storage and reduce write overhead:
Monitor Slow Queries: Enable MongoDB’s slow query log to identify other problematic queries:
Aggregation pipelines, used for complex data processing, can also benefit from indexes. Consider an aggregation to calculate total sales per customer:
Schema validation is especially valuable when:
Use explain(“executionStats“) for aggregations:
Analysis:
The $match stage filters on status. Create an index to optimize it:
MongoDB Atlas makes it easy to get started. But to truly deliver performance, consistency, and long-term value, ongoing optimization is essential. In this guide, we explored four key areas:
Re-run the aggregation with explain():
Analysis:
Notes on Aggregation Indexes
Stage | Description |
$match | Can use indexes (similar to find()), especially when placed at the beginning of the pipeline. |
$sort | Can use indexes if the sort field is indexed and a significant number of documents have already been filtered (e.g., via $match). |
$geoNear | Must be placed at the beginning of the pipeline and requires a geospatial index; otherwise, an error will occur. |
$search | A stage specific to Atlas Search; uses Atlas Search indexes (not regular MongoDB indexes). |
$limit | Can be optimized with $sort under appropriate conditions, leveraging indexes. |
Poor index design (such as using low-selectivity fields or creating redundant indexes) can lead to suboptimal query performance, slow down write operations, and consume excessive storage.
Design for High Selectivity:
High-selectivity fields are those with low repetition and high uniqueness (e.g., customer_id, email, order_id). The higher the selectivity, the better the index can filter data. For example:
Apply the ESR Rule (Equality, Sort, Range):
When designing compound indexes, follow the order Equality → Sort → Range to maximize index efficiency:
Avoid Over-Indexing:
Too many indexes increase storage usage and slow down write performance. Use db.collection.stats() to monitor index size.
It is recommended to periodically review indexes:
Use db.collection.explain() and the Atlas Performance Advisor to identify unused indexes and consider removing them.
Use Covered Queries: If a query only needs indexed fields, ensure it’s covered by the index to avoid fetching documents:
Regular Maintenance: Periodically review slow query logs and update indexes based on application changes.
Indexes are critical for MongoDB performance, especially for large datasets. By analyzing queries with explain(“executionStats”), developers can identify inefficiencies, create targeted indexes, and verify improvements. For aggregations pipelines, placing $match early and supporting it with proper indexes leads to faster, more efficient processing.
Key takeaways:
At Delbridge, we help teams get the most out of MongoDB by:
Whether you’re troubleshooting performance issues or building a new data model, our experts can help.
Book a MongoDB Index Review with Delbridge or reach out to our team to get started.
