You are offline

Switched to Home view. Returning to your route when internet connects.

Back to Knowledge Center
Coding
Published:Jul 7, 2026
Updated:Aug 1, 2026
4 min read

Optimizing Database Queries with MongoDB Compound Indexes

Optimizing Database Queries with MongoDB Compound Indexes
Karan Singh

Karan Singh

DevOps Architect & Infrastructure Mentor

Karan Singh is a Senior DevOps Architect & Open Source Advocate. He writes about Docker containerization, database schema optimizations, and microservices scaling.

MongoDB is a highly flexible document database, but as your collections scale to millions of records, query performance can degrade rapidly. If MongoDB has to scan every single document in a collection (a full collection scan or COLLSCAN) to satisfy a query, response times will balloon from milliseconds to seconds. Creating compound indexes is one of the most powerful optimization strategies to speed up multi-field lookups.

1. What is a Compound Index?

In MongoDB, an index is a sorted data structure that stores a small portion of the collection's data in an easy-to-traverse B-tree format. While a single-field index sorts documents by one field, a compound index supports queries that filter or sort by multiple fields. For example, if you frequently query user credits based on their status and department, you can define a compound index:

Javascript
// Schema setup in Mongoose
userSchema.index({ status: 1, department: 1 });

The ordering of the fields in a compound index is critical. MongoDB sorts the keys first by the first field (status), and then by the second field (department).

2. The Equality, Sort, Range (ESR) Rule

To design compound indexes that satisfy both filter constraints and sorting orders, always follow the ESR rule:

    [object Object]
Javascript
// Index designed for query: { category: 'Coding', rating: { $gt: 4 } } sort by { createdAt: -1 }
blogSchema.index({ category: 1, createdAt: -1, rating: 1 });
3. Analyzing Query Performance with explain()

Never guess if your indexes are working. Use MongoDB's explain() command to view query plans and execution statistics:

Javascript
const explainPlan = await Blog.find({ category: 'Coding' })
  .sort({ createdAt: -1 })
  .explain('executionStats');
console.log(explainPlan.executionStats);

Look for these indicators:

    [object Object]
4. Conclusion

Designing smart compound indexes ensures MongoDB retrieves documents in constant time, even as database size grows. Adhering to the ESR rule and verifying index execution paths using explain() keeps backend services responsive and server loads minimal.

Comments (4)

Karan Singh
Karan Singh05:42 PM

This is an incredibly helpful article. The step-by-step guidance is really clear!

Aditya Verma
Aditya Verma09:08 AM

Go concurrency is so powerful. Goroutines make building high-performance backends feel simple.

Neha Desai
Neha Desai12:18 PM

Docker containers are essential now. Knowing containerization is a must-have DevOps skill.

Karan Singh
Karan Singh07:28 PM

Integrating vector databases and AI APIs is where all the engineering demand is in 2026.