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 does combining comparison operators in MongoDB allow you to do?
It allows you to filter documents by applying multiple conditions on the same field, such as finding values greater than one number and less than another.
Click to reveal answer
beginner
How do you combine comparison operators like $gt and $lt in a MongoDB query?
You put them inside the same field object, for example: { age: { $gt: 20, $lt: 30 } } to find ages greater than 20 and less than 30.
Click to reveal answer
intermediate
What is the difference between using $and operator and combining comparison operators inside one field in MongoDB?
$and combines multiple conditions that can be on different fields, while combining comparison operators inside one field applies multiple conditions on the same field.
Click to reveal answer
beginner
Example: How to find documents where price is between 10 and 50 in MongoDB?
Use: { price: { $gte: 10, $lte: 50 } } which means price greater than or equal to 10 and less than or equal to 50.
Click to reveal answer
intermediate
Can you combine $ne (not equal) with other comparison operators on the same field in MongoDB?
Yes, for example: { score: { $ne: 100, $gt: 50 } } finds scores not equal to 100 and greater than 50.
Click to reveal answer
How do you write a MongoDB query to find documents where age is greater than 18 and less than 30?
Combine $ne and $lte inside one field object = { score: { $ne: 50, $lte: 100 } } [OK]
Hint: Put all comparison operators inside one field object [OK]
Common Mistakes:
Placing $lte outside the field object causes syntax error
Using $gte instead of $lte changes the condition
Separating operators into different objects breaks query
3. Given the collection products with documents like { price: 150, stock: 30 }, what will the query { price: { $gt: 100, $lt: 200 }, stock: { $gte: 20, $lte: 40 } } return?
medium
A. Documents where price is greater than 100 or stock is less than 40
B. Documents where price is exactly 100 and stock is exactly 20
C. Documents where price is between 100 and 200, and stock is between 20 and 40
D. Documents where price is less than 100 and stock is greater than 40
Solution
Step 1: Understand the combined conditions on price and stock
The query uses $gt and $lt on price to find values strictly between 100 and 200, and $gte and $lte on stock to find values between 20 and 40 inclusive.
Step 2: Interpret the logical AND behavior of MongoDB queries
Both conditions must be true for a document to match, so only documents with price in (100,200) and stock in [20,40] are returned.
Final Answer:
Documents where price is between 100 and 200, and stock is between 20 and 40 -> Option C
Quick Check:
Combined conditions on fields = Documents where price is between 100 and 200, and stock is between 20 and 40 [OK]
Hint: All conditions on different fields combine with AND logic [OK]
Common Mistakes:
Thinking conditions combine with OR instead of AND
Confusing inclusive ($gte/$lte) with exclusive ($gt/$lt)
Assuming exact matches instead of ranges
4. Identify the error in this MongoDB query: { quantity: { $gt: 10 }, $lt: 50 }
medium
A. The query is correct and will work as expected
B. The $gt operator should be $gte
C. The field name 'quantity' is missing
D. The $lt operator is outside the field object and causes syntax error
Solution
Step 1: Analyze the placement of comparison operators
The $lt operator is placed outside the quantity field object, which is invalid syntax in MongoDB queries.
Step 2: Correct syntax requires all operators on the same field inside one object
Both $gt and $lt must be inside the quantity object like { quantity: { $gt: 10, $lt: 50 } }.
Final Answer:
The $lt operator is outside the field object and causes syntax error -> Option D
Quick Check:
All operators must be inside field object = The $lt operator is outside the field object and causes syntax error [OK]
Hint: Keep all comparison operators inside the same field object [OK]
Common Mistakes:
Placing operators outside the field object
Confusing $gt with $gte unnecessarily
Assuming query works despite syntax error
5. You want to find documents where rating is greater than 3 but not equal to 5, and reviews are between 10 and 100 inclusive. Which query correctly combines these conditions?