0PricingLogin
MongoDB Academy · Lesson

How MongoDB B-Tree Indexes Work

Learners will trace how MongoDB stores index entries in a B-tree and how the query planner walks the tree to satisfy a filter.

Why Indexes Exist

Without an index, MongoDB must scan every document in a collection to satisfy a query—this is called a collection scan (COLLSCAN). On a collection with millions of documents, a COLLSCAN can take seconds or even minutes. An index is a separate, ordered data structure that lets MongoDB jump directly to the matching documents in microseconds.

The B-Tree Data Structure

MongoDB uses a B-tree (balanced tree) to store index entries. A B-tree is organised as a hierarchy of nodes: a root node at the top, internal nodes in the middle, and leaf nodes at the bottom. Every node holds multiple key-value pairs and pointers to child nodes. The tree stays balanced—all leaf nodes are at the same depth—so lookups always take the same number of steps regardless of which value you search for.

All lessons in this course

  1. How MongoDB B-Tree Indexes Work
  2. Creating Single-Field and Compound Indexes
  3. Index Properties: Unique, Sparse, Partial, TTL
  4. Reading explain() Output to Diagnose Queries
← Back to MongoDB Academy