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
Query Filter Syntax in MongoDB
📖 Scenario: You are managing a small online bookstore database. You want to find books based on certain criteria like author, price, and availability.
🎯 Goal: Build MongoDB queries step-by-step to filter books by author, price, and stock status using query filter syntax.
📋 What You'll Learn
Create a collection named books with sample book documents
Add a filter variable to specify query conditions
Write a MongoDB query using the filter to find matching books
Complete the query with a projection to show only title and author
💡 Why This Matters
🌍 Real World
Filtering data is a common task in databases to find specific records quickly, such as finding books by a certain author or price range.
💼 Career
Understanding query filter syntax is essential for database administrators, backend developers, and data analysts who work with MongoDB or similar NoSQL databases.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a variable called books and assign it a list of three book documents with these exact fields and values: { title: "The Great Gatsby", author: "F. Scott Fitzgerald", price: 10, inStock: true }, { title: "1984", author: "George Orwell", price: 15, inStock: false }, and { title: "To Kill a Mockingbird", author: "Harper Lee", price: 12, inStock: true }.
MongoDB
Hint
Use a list of dictionaries to represent the books collection with exact field names and values.
2
Create a filter to find books by author "George Orwell"
Create a variable called filter and assign it a dictionary with the key author and value "George Orwell" to filter books by this author.
MongoDB
Hint
Use a dictionary with the key author and value "George Orwell" to create the filter.
3
Write a MongoDB query using the filter to find matching books
Create a variable called query_result and assign it the result of filtering the books list using the filter variable. Use a list comprehension that checks if each book's author matches the filter's author.
MongoDB
Hint
Use a list comprehension to select books where the author matches the filter's author.
4
Complete the query with a projection to show only title and author
Create a variable called final_result and assign it a list of dictionaries containing only the title and author fields from each book in query_result. Use a list comprehension to extract these fields.
MongoDB
Hint
Use a list comprehension to create a new list with dictionaries containing only title and author from each book.
Practice
(1/5)
1. What does a MongoDB query filter do? { age: 25 }
easy
A. Deletes documents where age is 25
B. Updates documents to set age to 25
C. Selects documents where the age field is exactly 25
D. Creates a new document with age 25
Solution
Step 1: Understand the role of a query filter
A query filter in MongoDB is used to find documents that match certain conditions, not to modify or delete them.
Step 2: Analyze the filter { age: 25 }
This filter matches documents where the field age has the value 25 exactly.
Final Answer:
Selects documents where the age field is exactly 25 -> Option C
Quick Check:
Query filter = select matching documents [OK]
Hint: Filters pick matching data, not modify or delete [OK]
Common Mistakes:
Confusing filter with update or delete commands
Thinking filter creates or changes documents
Assuming filter matches partial or range values without operators
2. Which of these is the correct syntax to find documents where score is greater than 80?
easy
A. { score > 80 }
B. { score: gt 80 }
C. { $score: { gt: 80 } }
D. { score: { $gt: 80 } }
Solution
Step 1: Recall MongoDB operator syntax
MongoDB uses JSON objects with operators starting with a dollar sign, like $gt for 'greater than'.
Step 2: Check each option
{ score: { $gt: 80 } } uses { score: { $gt: 80 } } which is correct syntax. Others miss the dollar sign or use invalid syntax.
Final Answer:
{ score: { $gt: 80 } } -> Option D
Quick Check:
Use $gt for greater than in filters [OK]
Hint: Operators start with $ inside field object [OK]