0
0
MongoDBquery~10 mins

How MongoDB indexes work (B-tree mental model) - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - How MongoDB indexes work (B-tree mental model)
Start: Query for value
Check root node keys
Compare query value with keys
Go to child node based on range
Repeat check keys in child node
Reach leaf node with pointers to documents
Return matching documents
End
The query starts at the root node of the B-tree, compares the value with keys to decide which child node to follow, repeats this until reaching a leaf node that points to matching documents.
Execution Sample
MongoDB
db.collection.find({age: 30}).explain('executionStats')
This query searches for documents where the age is 30 using an index on the age field.
Execution Table
StepNode TypeKeys in NodeQuery Value ComparedDecisionNext Action
1Root[20, 40, 60]3030 > 20 and < 40Go to child node between 20 and 40
2Child[25, 35]3030 > 25 and < 35Go to child node between 25 and 35
3Leaf[28, 30, 33]30Found 30Return pointers to documents with age=30
4EndN/AN/AQuery completeReturn results to user
💡 Reached leaf node containing the key 30, so query returns matching documents.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Current NodeRootChild between 20 and 40Child between 25 and 35Leaf with keys [28,30,33]Leaf node reached
Query Value3030303030
DecisionN/AGo to child node between 20 and 40Go to child node between 25 and 35Found 30Return documents
Key Moments - 2 Insights
Why does the search start at the root node and not directly at the leaf?
The root node contains keys that help decide which child node to follow. This step-by-step narrowing down is how the B-tree efficiently finds the correct leaf node, as shown in steps 1 and 2 of the execution_table.
What happens if the query value is not found exactly in the keys?
The search still follows the child node whose key range would contain the value if it existed. Eventually, it reaches a leaf node where no exact match is found, and the query returns no results. This is implied by the decision steps in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at Step 2, which keys are in the child node?
A[25, 35]
B[20, 40, 60]
C[28, 30, 33]
D[30, 40, 50]
💡 Hint
Check the 'Keys in Node' column at Step 2 in the execution_table.
At which step does the query find the exact key matching the query value?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Decision' column where it says 'Found 30'.
If the query value was 50 instead of 30, which child node would the root node direct the search to?
AChild node between 20 and 40
BChild node between 60 and above
CChild node between 40 and 60
DLeaf node directly
💡 Hint
Refer to Step 1's decision logic comparing query value with root keys.
Concept Snapshot
MongoDB indexes use a B-tree structure.
Queries start at the root node.
Keys in nodes guide which child node to follow.
Search continues down until leaf nodes.
Leaf nodes point to matching documents.
This makes searches fast and efficient.
Full Transcript
When MongoDB uses an index, it organizes keys in a B-tree structure. The search begins at the root node, which contains keys dividing the data ranges. The query value is compared with these keys to decide which child node to visit next. This process repeats, moving down the tree nodes, narrowing the search range. Eventually, the search reaches a leaf node that contains pointers to the actual documents matching the query. If the key is found, those documents are returned. If not, the query returns no results. This step-by-step traversal makes searching large datasets fast and efficient.