Query Patterns That Cause Collection Scans in MongoDB
📖 Scenario: You are working with a MongoDB database for a small online bookstore. The database has a collection called books that stores information about each book, including its title, author, year of publication, and genre.Sometimes, queries run slowly because MongoDB has to scan the entire collection instead of using an index. This happens when queries use patterns that do not match any index.
🎯 Goal: Learn to write queries that cause collection scans in MongoDB by using patterns that do not use indexes. This will help you understand why some queries are slow and how to identify them.
📋 What You'll Learn
Create a books collection with specific documents.
Add an index on the author field.
Write a query that causes a collection scan by searching on a non-indexed field.
Write a query that causes a collection scan by using a regex pattern that cannot use the index.
💡 Why This Matters
🌍 Real World
Understanding which queries cause collection scans helps developers optimize database performance by creating appropriate indexes.
💼 Career
Database administrators and backend developers must know how to identify slow queries caused by collection scans to improve application responsiveness.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a variable called books and assign it an array of three documents with these exact fields and values: { title: "The Hobbit", author: "J.R.R. Tolkien", year: 1937, genre: "Fantasy" }, { title: "1984", author: "George Orwell", year: 1949, genre: "Dystopian" }, and { title: "To Kill a Mockingbird", author: "Harper Lee", year: 1960, genre: "Fiction" }. Then insert the documents into the books collection using db.books.insertMany(books).
MongoDB
Hint
Use an array with three objects exactly as shown, then db.books.insertMany(books);
2
Create an index on the author field
Write a line of code that creates an index on the author field of the books collection using db.books.createIndex({ author: 1 }).
MongoDB
Hint
Use db.books.createIndex with { author: 1 } to create the index.
3
Write a query that causes a collection scan by searching on genre
Write a query using db.books.find() to find all books where genre is exactly "Fantasy". This query will cause a collection scan because genre is not indexed.
MongoDB
Hint
Use db.books.find with a filter on genre equal to "Fantasy".
4
Write a query that causes a collection scan using a regex on title
Write a query using db.books.find() to find all books where the title starts with the letter "T" using a regex: { title: /^T/ }. This query will cause a collection scan because title is not indexed and the regex cannot use an index.
MongoDB
Hint
Use db.books.find with a regex filter on title starting with T.
Practice
(1/5)
1. Which of the following query patterns in MongoDB is most likely to cause a collection scan?
easy
A. Querying a field without an index
B. Querying a field with a single-field index
C. Using an indexed field with equality operator
D. Using a covered index query
Solution
Step 1: Understand collection scan causes
A collection scan happens when MongoDB must check every document because no index supports the query.
Step 2: Analyze query patterns
Querying a field without an index forces MongoDB to scan the entire collection, unlike queries using indexed fields.
Final Answer:
Querying a field without an index -> Option A
Quick Check:
No index = collection scan [OK]
Hint: No index on field? Expect collection scan [OK]
Common Mistakes:
Assuming all queries use indexes automatically
Confusing covered index with collection scan
Thinking equality queries always cause scans
2. Which of the following MongoDB query syntaxes is correct and avoids collection scans by using an index on the field age?
easy
A. db.users.find({ age: { $gt: 25 } })
B. db.users.find({ $where: 'this.age > 25' })
C. db.users.find({ age: { $regex: '^2' } })
D. db.users.find({ $text: { $search: '25' } })
Solution
Step 1: Check query syntax correctness
All queries are syntactically valid, but some use operators that prevent index use.
Step 2: Identify index-friendly query
Query with { age: { $gt: 25 } } uses range operator on indexed field, avoiding collection scan. Queries with $where or $regex often cause scans.
Final Answer:
db.users.find({ age: { $gt: 25 } }) -> Option A
Quick Check:
Range query on indexed field = no scan [OK]
Hint: Use simple operators on indexed fields to avoid scans [OK]
Common Mistakes:
Using $where causes collection scan
Assuming $regex always uses index
Confusing $text search with field index
3. Given a collection products with an index on category, what will be the result of this query?