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
Recall & Review
beginner
What is a collection scan in MongoDB?
A collection scan is when MongoDB reads every document in a collection to find those that match a query. It checks documents one by one without using an index.
Click to reveal answer
beginner
How does MongoDB use indexes to scan documents?
MongoDB uses indexes to quickly find documents matching a query without scanning the whole collection. It looks up index entries to locate documents efficiently.
Click to reveal answer
intermediate
What is the difference between a collection scan and an index scan?
A collection scan reads every document in the collection, while an index scan uses an index to find matching documents faster, scanning fewer documents.
Click to reveal answer
beginner
Why can collection scans be slow in MongoDB?
Collection scans can be slow because MongoDB must check every document in the collection, which takes more time as the collection grows larger.
Click to reveal answer
intermediate
How can you check if MongoDB is using a collection scan or an index scan?
You can use the explain() method on a query. It shows whether MongoDB used a collection scan or an index scan to find documents.
Click to reveal answer
What does MongoDB do during a collection scan?
AReads every document in the collection
BUses an index to find documents
CDeletes documents
DCreates a new collection
✗ Incorrect
A collection scan means MongoDB reads every document to find matches.
Which method helps you see if MongoDB used an index or collection scan?
Ainsert()
Bfind()
Cexplain()
Dupdate()
✗ Incorrect
The explain() method shows the query plan including scan type.
Why are index scans faster than collection scans?
AThey read fewer documents using the index
BThey delete documents faster
CThey create indexes automatically
DThey scan the whole collection
✗ Incorrect
Index scans use indexes to read fewer documents, speeding up queries.
What happens if no index matches a query in MongoDB?
AMongoDB creates an index automatically
BMongoDB performs a collection scan
CMongoDB returns an error
DMongoDB skips the query
✗ Incorrect
Without a matching index, MongoDB scans all documents to find matches.
Which of these is a sign of a slow query in MongoDB?
AUsing find()
BUsing an index scan
CUsing explain()
DUsing a collection scan
✗ Incorrect
Collection scans can slow queries because they read every document.
Explain how MongoDB scans documents when answering a query.
Think about how MongoDB finds matching documents with and without indexes.
You got /3 concepts.
Describe why using indexes is important for MongoDB query performance.
Consider what happens when MongoDB has to check every document.
You got /3 concepts.
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
Step 1: Understand MongoDB scanning without indexes
Without indexes, MongoDB must check each document to find matches.
Step 2: Recognize the scanning method
This means MongoDB performs a full collection scan, checking documents one by one.
Final Answer:
It scans every document one by one. -> Option D
Quick Check:
No index means full scan = It scans every document one by one. [OK]
Hint: No index means MongoDB scans all documents [OK]
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
Step 1: Recall MongoDB index creation syntax
The correct syntax uses createIndex with a document specifying field and order.
Step 2: Match syntax to options
db.collection.createIndex({age: 1}) uses {age: 1} which means ascending order, the correct format.
Final Answer:
db.collection.createIndex({age: 1}) -> Option A
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
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.
Step 2: Identify matching documents
Documents with age 30 and 35 satisfy the condition, so MongoDB scans only these two.
Final Answer:
Only documents with age 30 and 35 -> Option A
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
Step 1: Check index field correctness
If the index is not on the age field, MongoDB cannot use it for this query.
Step 2: Confirm MongoDB capabilities
MongoDB supports indexes on numeric fields and range queries ($lt, $gt, etc.). The provided query syntax is correct.
Final Answer:
The index was created on a different field, not age. -> Option B
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
Step 1: Analyze query with multiple conditions
The query filters on status and score. Only status is indexed.
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.
Final Answer:
Because score is not indexed, MongoDB scans documents matching status to check score condition. -> Option C
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