DEV Community

Priyank Agrawal
Priyank Agrawal

Posted on • Edited on

3

🔍 How MongoDB Indexing Works Internally: B+Tree, B- Tree Structure, Performance Impact & Best Practices

Indexing is the backbone of database performance. In MongoDB, indexes are not just a luxury—they're essential for building scalable, performant applications. But how do they really work under the hood?

In this deep dive, we'll explore:

  • The core architecture of MongoDB indexes
  • Internal algorithms and data structures
  • How indexing affects read vs write operations
  • Practical indexing strategies and best practices

🧠 What Is Indexing in MongoDB?

An index in MongoDB is a special data structure that stores a subset of a collection's data in an efficient, sorted format. This allows the database engine to locate documents without scanning the entire collection.

MongoDB automatically creates an index on the _id field. You can (and should) define additional indexes to optimize specific queries.


🌳 Internal Index Structure: B-Trees

MongoDB uses B-Trees to manage its indexes. Here's how they work:

🔍 What's a B-Tree?

  • A self-balancing tree data structure
  • Keeps data sorted for logarithmic-time lookups
  • Both internal and leaf nodes can store data
  • Supports range queries, prefix matching, and sorted access

💡 Why B-Trees in MongoDB?

  • Enables fast insertions, deletions, and lookups (O(log n))
  • Allows range scans for $gte, $lte, $in, etc.
  • Efficient balancing as data changes
  • Well-suited for disk-based storage systems

🔁 Index Lifecycle: How MongoDB Maintains Indexes

Every time a document is inserted, updated, or deleted, all relevant indexes must be updated. Here's what happens internally:

✅ Insert:

  • MongoDB finds the correct location in the B-Tree
  • A new key is inserted
  • Tree rebalancing may occur if necessary

✏️ Update:

  • If the indexed field changes:
    • MongoDB updates the key in the tree
    • May involve removing and reinserting keys
  • This causes write amplification if there are many indexes

❌ Delete:

  • Keys are removed from all applicable indexes

⚠️ Indexes help read performance but can affect write performance due to additional maintenance operations.


⚡ Types of Indexes in MongoDB and Their Internals

Index Type Internals Use Case
Single Field B-tree (WiredTiger storage engine) Basic filters and sorts
Compound B-tree with multi-part keys Queries with multiple filters/sorts
Multikey B-tree with separate entry per array element Indexing arrays
Text Index B-tree of lexicographically sorted terms Full-text search
TTL Index Single field index + background deletion proc Auto-expiring documents
Sparse/Partial B-tree with filtered document set Conditional indexing
Geospatial B-tree (2d) or B-tree+S2 (2dsphere) Location-based queries
Hashed B-tree of hashed values Hash-based sharding

📊 Query Execution with Indexes

🧠 The Query Planner

MongoDB's query optimizer evaluates different query execution plans using available indexes. It selects the most efficient plan based on:

  • Index selectivity (how well an index narrows results)
  • Query predicates and their matching to indexes
  • Sort requirements and whether indexes can satisfy them
  • Statistics about data distribution

The optimizer may periodically re-evaluate plans as collection data changes over time.

🔀 Index Intersection

MongoDB can use multiple indexes to resolve a single query when:

  • Different indexes match different query conditions
  • The intersection would be more selective than using a single index
  • No single index exists that fully covers the query

However, index intersection isn't always more efficient and has its limitations, especially with large collections.

📦 Covered Queries

If all fields required by the query (both in the query criteria and in the projection) are included in an index, MongoDB can fulfill the query using only the index without accessing the documents—these "covered" queries are extremely fast!

// Example of a covered query (assuming there's an index on {age: 1, name: 1})
db.users.find({ age: 30 }, { age: 1, name: 1, _id: 0 })
Enter fullscreen mode Exit fullscreen mode

⚖️ Read vs. Write Trade-offs

✅ When Indexes Help:

  • High-frequency reads
  • Filters and sorts
  • Joins using $lookup
  • Range queries and pagination

❌ When Indexes Hurt:

  • High-frequency writes (inserts/updates)
  • Frequent indexed field changes
  • Low cardinality fields (e.g., gender)

Rule of Thumb: Use indexes on collections primarily accessed for reads. Be strategic with indexing on collections with high write throughput.


🧱 WiredTiger Storage Engine & Indexing

MongoDB's default engine, WiredTiger:

  • Stores collection data in separate data files
  • Uses B-trees for the _id index and all other indexes
  • Each index is maintained in its own file

🧬 Compression:

  • Prefix compression on index keys
  • Block compression for data
  • Reduces disk usage, improves cache efficiency

🛠 Hidden & Background Builds

  • Foreground: Locks collection (faster, blocking)
  • Background: Non-blocking (slower, safe for production)
  • Hidden indexes: Can be tested before making visible to the query planner

✅ Indexing Best Practices

  1. Index fields used in filtering and sorting
  2. Avoid indexing low-cardinality fields
  3. Keep indexes narrow (fewer fields)
  4. Use compound indexes in the correct field order
  5. Use .explain() with verbosity modes to validate
  6. Monitor index usage with MongoDB Atlas or profiler
  7. Drop unused indexes
   db.collection.dropIndex("index_name")
Enter fullscreen mode Exit fullscreen mode
  1. Balance indexing on write-heavy collections

🧪 Real-World Example: Compound Index

// Create a compound index
db.orders.createIndex({ customerId: 1, createdAt: -1 })

// Efficient for:
db.orders.find({ customerId: "123" }).sort({ createdAt: -1 })

// Not efficient for:
db.orders.find({ createdAt: { $gte: ISODate() } })
Enter fullscreen mode Exit fullscreen mode

🧠 Developer Insight

"Use indexing strategically by understanding your access patterns. For read-heavy collections, comprehensive indexing can dramatically improve performance. For write-heavy collections, be selective to avoid unnecessary index maintenance overhead."


📘 Conclusion

MongoDB indexing is a sophisticated system built on B-tree data structures, efficient compression techniques, and intelligent query planning.

By understanding:

  • B-Tree mechanics and limitations
  • Read/write trade-offs
  • Query planner decisions

You can architect highly optimized applications that balance performance across various workloads.


👨‍💻 Author: Priyank Agrawal

Software Developer | Node.js | MongoDB
🔗 Dev.to Profile
🔗 LinkedIn


📌 Follow for More

If you found this useful, follow me on Dev.to or connect with me on LinkedIn for more deep-dive technical articles.

Heroku

Build AI apps faster with Heroku.

Heroku makes it easy to build with AI, without the complexity of managing your own AI services. Access leading AI models and build faster with Managed Inference and Agents, and extend your AI with MCP.

Get Started

Top comments (6)

Collapse
 
loukilaymen profile image
Aymen Loukil

Nice post explaining the internals of MongoDB indexing, thanks for sharing!
I did a video explaining how to test the impact of an index here
dev.to/loukilaymen/how-to-test-the...

Collapse
 
priyank_agrawal profile image
Priyank Agrawal

Thank you so much.

Collapse
 
loukilaymen profile image
Aymen Loukil

Welcome!
I'm looking for testers who can give feedback on Mongo Pilot. Let me know if you are interested ;)

Thread Thread
 
priyank_agrawal profile image
Priyank Agrawal

Ok I will let you know. PIng me on linkedin.

Collapse
 
gitesh_pareek_1018774b56f profile image
Gitesh Pareek

Very Informative!

Collapse
 
priyank_agrawal profile image
Priyank Agrawal

Thanks brother

ITRS image

See What Users Experience in The Browser — Anywhere, Anytime

Simulate logins, checkouts, and payments on SaaS, APIs, and internal apps. Catch issues early, baseline web performance, and stay ahead of incidents. Easily record user journeys right from your browser.

Start Free Trial

👋 Kindness is contagious

Discover this thought-provoking article in the thriving DEV Community. Developers of every background are encouraged to jump in, share expertise, and uplift our collective knowledge.

A simple "thank you" can make someone's day—drop your kudos in the comments!

On DEV, spreading insights lights the path forward and bonds us. If you appreciated this write-up, a brief note of appreciation to the author speaks volumes.

Get Started