
Learn how to install MongoDB (Community or Enterprise) on RHEL 10 / CentOS 10 with step‑by‑step repository setup, CLI commands, SELinux tips, and troubleshooting.” Table of Contents
Avoid MongoDB performance pitfalls by learning five common indexing mistakes that slow down your database. Includes CLI examples, charts, and actionable fixes.
MongoDB is renowned for its flexibility and scalability as a NoSQL database, but when it comes to performance, indexing is everything. A well-designed index can make queries blazingly fast, while poor indexing can quietly strangle performance, bloat storage, and frustrate developers.
In this post, we’ll explore five common MongoDB indexing mistakes that hurt performance and scalability. We’ll look at the symptoms, why they happen, how to fix them, and show real examples with CLI commands and performance tips.
🔍 The Mistake |
One of the most basic yet common mistakes is failing to create indexes on fields used frequently in find(), sort(), update(), or aggregation queries. MongoDB will scan every document in a collection (a collection scan) if no appropriate index exists.
⚠️ Why It’s a Problem |
|
|
|
🛠️ How to Fix It |
Use the createIndex() method to add indexes to fields that are regularly filtered, sorted, or joined on.
📌 Example |
db.users.createIndex({ email: 1 })
If you frequently search by email, this index makes those queries significantly faster.
📊 CLI Example |
Check for slow queries using the MongoDB Atlas Performance Advisor or system profiler:
db.system.profile.find({ millis: { $gt: 100 } }).sort({ ts: -1 }).limit(5)
🔍 The Mistake |
Some developers try to solve all performance problems by adding more indexes. However, every write operation (insert, update, delete) must update every index, which introduces overhead.
⚠️ Why It’s a Problem |
|
|
|
🛠️ How to Fix It |
|
|
db.orders.find({ userId: 123, status: "delivered" })
📊 Cost of Index Overhead |
| Index Count | Write Latency Impact | Storage Increase |
|---|---|---|
| 1–3 | Minimal | ~10% |
| 4–6 | Noticeable | ~20–30% |
| 7+ | High | 50%+ |
💡Actual values vary based on workload and index types. |
🛠️ How to Fix It |
|
|
db.collection.dropIndex("index_name")
Use $indexStats to see index usage:
db.collection.aggregate([ { $indexStats: {} } ])
🔍 The Mistake |
Relying on multiple single-field indexes instead of a compound index for multi-field queries causes MongoDB to perform index intersection, which is slower.
⚠️ Why It’s a Problem |
|
|
|
🛠️ How to Fix It |
Build compound indexes that reflect the structure of your most common queries.
📌 Query Examples |
db.orders.find({ userId: 123, status: "delivered" })
Recommended index:
db.orders.createIndex({ userId: 1, status: 1 })
If sorting is involved:
db.orders.find({ userId: 123 }).sort({ createdAt: -1 })
Use:
db.orders.createIndex({ userId: 1, createdAt: -1 })
⚡ Pro Tip |
Order of fields matters in compound indexes. Use the Equality → Sort → Range (ESR) rule:
|
|
|
🔍 The Mistake |
Creating indexes on low-cardinality fields (e.g., status, gender, isActive) that don’t help filter documents meaningfully. MongoDB ends up scanning large portions of the collection anyway.
⚠️ Why It’s a Problem |
|
|
|
📊 Cardinality Breakdown |
| Field | Type | Cardinality | Useful Index? |
|---|---|---|---|
gender | String (M/F/X) | Low | ❌ No |
email | String (Unique) | High | ✅ Yes |
status | Enum (5 types) | Medium | ⚠️ Maybe |
userId | ObjectId | Unique | ✅ Yes |
🛠️ How to Fix It |
|
|
📌 Example: |
db.orders.createIndex({ userId: 1, status: 1 })
Here, status is low-cardinality, but useful when combined with userId.
🔍 The Mistake |
Developers skip the explain() method and assume the query is using the right index. MongoDB’s query planner sometimes picks suboptimal indexes, especially when many indexes exist.
⚠️ Why It’s a Problem |
|
|
|
🛠️ How to Fix It |
|
📌 Example: |
db.orders.find({ userId: 123, status: "shipped" }).explain("executionStats")
Sample output:
"executionStats": {
"nReturned": 10,
"totalKeysExamined": 1000,
"totalDocsExamined": 1000,
...
}
If totalDocsExamined ≫ nReturned, your index is inefficient.
⚡ Pro Tip |
Use hint() to test index performance manually:
db.orders.find({ userId: 123 }).hint({ userId: 1 })
This is useful for debugging or when tuning multiple index options.
Here’s a simple routine you should adopt monthly (or during performance reviews):
|
db.collection.getIndexes()
|
db.collection.aggregate([{ $indexStats: {} }])
|
db.collection.find({ ... }).explain("executionStats")
|
db.collection.dropIndex("indexName")
|
Indexing is arguably the most critical factor in MongoDB performance. Avoiding these common mistakes will not only improve query speed and efficiency but also reduce infrastructure costs, minimize CPU usage, and improve user experiences.
Remember:
|
|
Did you find this article helpful? Your feedback is invaluable to us! Feel free to share this post with those who may benefit, and let us know your thoughts in the comments section below.

Learn how to install MongoDB (Community or Enterprise) on RHEL 10 / CentOS 10 with step‑by‑step repository setup, CLI commands, SELinux tips, and troubleshooting.” Table of Contents

Learn how to deploy a highly available PostgreSQL database cluster using Pacemaker on RHEL 9 or CentOS 9. This comprehensive guide covers installation, configuration, and testing

Learn how to deploy PostgreSQL database on Podman with this step-by-step guide. From installation to database setup, find out everything you need to know to
