0
0
MongoDBquery~10 mins

How MongoDB scans documents - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - How MongoDB scans documents
Start Query
Check for Index
Use Index
Fetch Docs
Apply Filters
Return Results
MongoDB first checks if an index can be used. If yes, it uses the index to find documents quickly. If no index is available, it scans all documents in the collection. Then it applies filters and returns the matching documents.
Execution Sample
MongoDB
db.users.find({age: {$gt: 30}})
This query finds all user documents where the age is greater than 30.
Execution Table
StepActionIndex Used?Documents ScannedDocuments MatchedResult
1Start query executionN/A00Query begins
2Check for index on 'age'Yes00Index found
3Use index to find candidate doc IDsYes00Candidate doc IDs found
4Fetch documents by IDsYes50Documents retrieved
5Apply filter age > 30Yes533 documents match
6Return matching documentsYes533 documents returned
7End query executionYes53Query complete
💡 Query ends after returning all documents matching age > 30 using index scan
Variable Tracker
VariableStartAfter Step 3After Step 5Final
Documents Scanned0055
Documents Matched0033
Index UsedN/AYesYesYes
Key Moments - 2 Insights
Why does MongoDB scan fewer documents when an index is used?
Because the index helps MongoDB quickly find only the documents that might match, so it doesn't have to look at every document in the collection (see execution_table steps 2 and 3).
What happens if there is no index on the queried field?
MongoDB will scan every document in the collection to check if it matches the query, which is slower (see concept_flow where 'No' leads to 'Full Collection Scan').
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does MongoDB apply the filter condition?
AStep 3
BStep 2
CStep 5
DStep 6
💡 Hint
Check the 'Action' column in execution_table where filter is applied
According to variable_tracker, how many documents were matched after applying the filter?
A3
B0
C5
DNone
💡 Hint
Look at 'Documents Matched' after Step 5 in variable_tracker
If there was no index on 'age', what would change in the execution flow?
AMongoDB would use the index anyway
BMongoDB would scan all documents in the collection
CMongoDB would return no results
DMongoDB would skip applying filters
💡 Hint
Refer to concept_flow where 'No' index leads to 'Full Collection Scan'
Concept Snapshot
MongoDB scans documents by first checking for an index on the queried field.
If an index exists, it uses it to quickly find matching documents.
Without an index, MongoDB scans every document in the collection.
Filters are applied after fetching documents.
Using indexes improves query speed by reducing scanned documents.
Full Transcript
When MongoDB runs a query, it first checks if there is an index on the field used in the query. If an index exists, MongoDB uses it to find matching document IDs quickly without scanning the entire collection. Then it fetches those documents and applies the filter condition to return only the matching documents. If no index exists, MongoDB scans every document in the collection to find matches, which is slower. This process helps MongoDB efficiently return query results.