October 16, 2025
Published by Usman Hussain
Unlocking the power of efficient data retrieval
Database performance can make or break an application's user experience. When your application starts slowing down due to inefficient queries, users notice, and they don't wait around. At BrightHR, we experienced this firsthand when our production systems were struggling with query performance issues that were directly impacting our users and infrastructure costs.
MongoDB indexing became our solution for dramatically improving query performance and ensuring our application could scale smoothly as our data grew. Think of indexes like a book's table of contents - instead of reading every page to find Chapter 5, you jump directly to page 87. Indexes create optimized data structures that point to document locations, transforming O(n) collection scans into O(log n) lookups.
In this article, we'll explore the fundamental benefits of MongoDB indexing through real-world examples from our production environment, examine different types of indexes, and share the concrete performance improvements we achieved.
Before implementing proper indexing, our production systems at BrightHR were experiencing several critical issues:
Imagine you have 1 million documents in your users collection, and you're looking for all users where CompanyId = 'ACME123'
Without an index:
With a CompanyId index in place:
Here's a comparison table showing the dramatic difference indexing makes:
| Collection Size | Without Index (O(n)) | With Index (O(log n)) | Speed Improvement |
|---|---|---|---|
| 1,000 docs | 1,000 reads | ~10 comparisons | 100x faster |
| 100,000 docs | 100,000 reads | ~17 comparisons | 5,900x faster |
| 1,000,000 docs | 1,000,000 reads | ~20 comparisons | 50,000x faster |
| 10,000,000 docs | 10,000,000 reads | ~23 comparisons | 435,000x faster |
The most basic type of index covers a single field and provides significant performance benefits for equality matches, range queries, and sorting operations.
// Create an index on the CompanyId field - as we implemented for user lookups
db.users.createIndex({ "CompanyId": 1 })
// This query now executes in milliseconds instead of seconds
db.users.find({ "CompanyId": "ACME123" })
Compound indexes cover multiple fields and are particularly powerful for queries that filter on multiple criteria or need to sort by multiple fields.
// Create a compound index on status and created_date
db.accidents.createIndex({ "status": 1, "created_date": -1 })
// Optimizes queries like our accident reporting system:
db.accidents.find({ "status": "pending" }).sort({ "created_date": -1 })
Text indexes enable full-text search capabilities, allowing users to search through text content efficiently.
// Create a text index for search functionality
db.articles.createIndex({
"title": "text",
"content": "text"
})
// Enable powerful search queries
db.articles.find({ $text: { $search: "mongodb indexing" } })
Create indexes on fields with high selectivity (many unique values). Our CompanyId indexes are highly effective because they create manageable result sets, whereas indexing a boolean field would provide minimal benefit.
We analyzed our application's most frequent queries and optimized specifically for those scenarios. Don't create indexes based on theoretical use cases—focus on real user behavior.
We regularly monitor our index usage to ensure they're providing value:
// Check index usage statistics
db.collection.aggregate([{ $indexStats: {} }])
While indexes dramatically improve read performance, they add overhead to write operations. We carefully balance this by focusing on high-impact queries rather than indexing every possible field.
Manual indexing approaches have significant problems:
We implemented MongoDB migrations to manage index creation systematically. Here's an example from our production codebase:
module.exports = {
async up(db) {
let note = db.collection('Note');
await note.createIndex(
{ Company: 1 },
// Create index in the background to avoid blocking operations
{ background: true, name: "idx_Company" }
);
console.log(`Created index idx_Company on collection ${note.name}`);
},
async down(db) {
let note = db.collection('Note');
await note.dropIndex("idx_Company");
}
};
• Total Request Charge consumption: 80-90% reduction (40-50M units → 2-5M units)

• Average Request Charge per operation: 91.7% reduction (200-300 units → 25-50 units)

• Query efficiency improvement: 12x faster individual database operations
• Resource consumption: Transformed from resource-hungry bottleneck to efficient system
• Average dashboard table loading time: 3 seconds → 200ms (15x faster)
• Cost savings: £50+ per day in reduced Azure Cosmos DB consumption
• Lower infrastructure costs: Reduced CPU and I/O operations
• Improved user experience: Faster response times across all features
• Better scalability: Handle increased load without additional investment
The monitoring charts below show the dramatic reduction in Mongo Request Charges before and after indexing implementation:
These results demonstrate that proper indexing isn't just a technical optimization—it's a business investment with measurable returns.
Despite their benefits, indexes aren't without trade-offs. They consume storage space, slow down write operations slightly, and require maintenance. The key is finding the right balance for your specific use case.
Over-indexing can be as problematic as under-indexing. Every index must be maintained during write operations, so having too many indexes can actually hurt performance. Focus on creating indexes that align with your query patterns and provide measurable benefits.
MongoDB indexing proved to be one of the most impactful optimizations we implemented in our database layer at BrightHR. The performance improvements were immediate and dramatic—transforming slow, unresponsive queries into lightning-fast operations that enhanced our users' experience significantly.
Our results speak for themselves: query times reduced from seconds to milliseconds, cost savings of £50+ per day, and a scalable foundation for future growth. The key to our success was taking a systematic approach through MongoDB migrations, focusing on real query patterns rather than theoretical optimizations, and continuously monitoring the impact.
The transformation from O(n) collection scans to O(log n) index lookups doesn't just improve individual query performance—it fundamentally changes what's possible with your application. Features that were previously too slow to be viable become instant and responsive.
For teams considering MongoDB indexing, our advice is simple: start with your most critical and frequent queries, implement proper migration tooling for consistency across environments, and measure the impact. The investment in time and planning pays dividends in performance, user experience, and reduced infrastructure costs.
By mastering MongoDB indexing, you're not just optimizing database performance—you're building a foundation for scalable, maintainable applications that can grow with your business needs while delivering the responsive experience users expect in today's competitive landscape.
Registered Office: Bright HR Limited, The Peninsula, Victoria Place, Manchester, M4 4FB. Registered in England and Wales No: 9283467. Tel: 0844 892 3928. I Copyright © 2024 BrightHR