Bird
Raised Fist0
MongoDBquery~10 mins

How MongoDB scans documents - Visual Walkthrough

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does MongoDB do when there is no index for a query?
easy
A. It uses a cached result from previous queries.
B. It immediately returns an error.
C. It only scans the first document.
D. It scans every document one by one.

Solution

  1. Step 1: Understand MongoDB scanning without indexes

    Without indexes, MongoDB must check each document to find matches.
  2. Step 2: Recognize the scanning method

    This means MongoDB performs a full collection scan, checking documents one by one.
  3. Final Answer:

    It scans every document one by one. -> Option D
  4. Quick Check:

    No index means full scan = It scans every document one by one. [OK]
Hint: No index means MongoDB scans all documents [OK]
Common Mistakes:
  • Thinking MongoDB returns an error without indexes
  • Assuming MongoDB uses cached results automatically
  • Believing MongoDB scans only part of the collection
2. Which of the following is the correct way to create an index on the field age in MongoDB?
easy
A. db.collection.createIndex({age: 1})
B. db.collection.createIndex('age')
C. db.collection.index({age: 'asc'})
D. db.collection.create({index: age})

Solution

  1. Step 1: Recall MongoDB index creation syntax

    The correct syntax uses createIndex with a document specifying field and order.
  2. Step 2: Match syntax to options

    db.collection.createIndex({age: 1}) uses {age: 1} which means ascending order, the correct format.
  3. Final Answer:

    db.collection.createIndex({age: 1}) -> Option A
  4. Quick Check:

    Index creation uses createIndex({field: order}) = db.collection.createIndex({age: 1}) [OK]
Hint: Use createIndex with {field: 1 or -1} for ascending/descending [OK]
Common Mistakes:
  • Using a string instead of an object for fields
  • Using incorrect method names like create or index
  • Passing field name without order direction
3. Given a collection with 3 documents: {name: 'A', age: 25}, {name: 'B', age: 30}, {name: 'C', age: 35}, and an index on age, what documents will MongoDB scan for the query {age: {$gt: 28}}?
medium
A. Only documents with age 30 and 35
B. All 3 documents
C. Only the document with age 35
D. No documents scanned because index is used

Solution

  1. Step 1: Understand query and index usage

    The query asks for documents where age is greater than 28. The index on age helps MongoDB find matching documents efficiently.
  2. Step 2: Identify matching documents

    Documents with age 30 and 35 satisfy the condition, so MongoDB scans only these two.
  3. Final Answer:

    Only documents with age 30 and 35 -> Option A
  4. Quick Check:

    Index filters to matching docs = Only documents with age 30 and 35 [OK]
Hint: Index narrows scan to matching documents only [OK]
Common Mistakes:
  • Thinking index scans no documents at all
  • Assuming all documents are scanned despite index
  • Selecting only one matching document incorrectly
4. You wrote this query: db.users.find({age: {$lt: 20}}) but it scans all documents even though you created an index on age. What is the likely problem?
medium
A. MongoDB does not support indexes on numeric fields.
B. The index was created on a different field, not age.
C. The query syntax is incorrect and causes full scan.
D. Indexes only work for equality, not range queries.

Solution

  1. Step 1: Check index field correctness

    If the index is not on the age field, MongoDB cannot use it for this query.
  2. Step 2: Confirm MongoDB capabilities

    MongoDB supports indexes on numeric fields and range queries ($lt, $gt, etc.). The provided query syntax is correct.
  3. Final Answer:

    The index was created on a different field, not age. -> Option B
  4. Quick Check:

    Index field mismatch causes full scan = The index was created on a different field, not age. [OK]
Hint: Check index field matches query field exactly [OK]
Common Mistakes:
  • Believing MongoDB can't index numeric fields
  • Assuming range queries never use indexes
  • Thinking query syntax is invalid when it is correct
5. You have a collection with 1 million documents and an index on status. You run db.collection.find({status: 'active', score: {$gt: 50}}). MongoDB scans many documents even though status is indexed. Why?
hard
A. Indexes only speed up queries with one condition.
B. MongoDB cannot use indexes with multiple conditions.
C. Because score is not indexed, MongoDB scans documents matching status to check score condition.
D. The query syntax is invalid and causes full scan.

Solution

  1. Step 1: Analyze query with multiple conditions

    The query filters on status and score. Only status is indexed.
  2. Step 2: Understand index usage with multiple fields

    MongoDB uses the index on status to find matching documents, but must scan those documents to check score because it is not indexed.
  3. Final Answer:

    Because score is not indexed, MongoDB scans documents matching status to check score condition. -> Option C
  4. Quick Check:

    Partial index use requires scanning for other conditions = Because score is not indexed, MongoDB scans documents matching status to check score condition. [OK]
Hint: Index only helps on indexed fields; others need document scan [OK]
Common Mistakes:
  • Thinking MongoDB can't use indexes with multiple conditions
  • Assuming query syntax is wrong when it is correct
  • Believing indexes speed up all conditions automatically