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 the $and operator do in MongoDB queries?
The $and operator combines multiple conditions and returns documents that satisfy all of them.
Click to reveal answer
beginner
How do you use comparison operators like $gt and $lt in MongoDB?
Comparison operators like $gt (greater than) and $lt (less than) are used inside query objects to filter documents based on field values.
Click to reveal answer
intermediate
Explain how to combine $or with comparison operators in a MongoDB query.
You use $or to specify multiple alternative conditions. Each condition can use comparison operators. The query returns documents matching any one of the conditions.
Click to reveal answer
beginner
What is the difference between $and and $or in MongoDB queries?
$and requires all conditions to be true for a document to match, while $or requires at least one condition to be true.
Click to reveal answer
beginner
How would you write a MongoDB query to find documents where age is greater than 25 and status is 'active'?
Use { $and: [ { age: { $gt: 25 } }, { status: 'active' } ] }. This finds documents where both conditions are true.
Click to reveal answer
Which operator in MongoDB returns documents that satisfy all given conditions?
A$nor
B$or
C$not
D$and
✗ Incorrect
The $and operator requires all conditions to be true for a document to match.
How do you check if a field 'score' is less than 50 in MongoDB?
A{ score: { $eq: 50 } }
B{ score: { $lt: 50 } }
C{ score: 50 }
D{ score: { $gt: 50 } }
✗ Incorrect
The $lt operator means 'less than', so { score: { $lt: 50 } } finds documents with score less than 50.
What does the following query do? { $or: [ { age: { $gt: 30 } }, { status: 'active' } ] }
AFinds documents where age is greater than 30 and status is 'active'
BFinds documents where age is less than 30 or status is not 'active'
CFinds documents where age is greater than 30 or status is 'active'
DFinds documents where age equals 30 or status equals 'active'
✗ Incorrect
The $or operator returns documents matching any one of the conditions.
Which operator would you use to combine conditions that all must be true?
A$and
B$or
C$nor
D$exists
✗ Incorrect
$and combines conditions that all must be true.
How do you write a query to find documents where 'price' is between 10 and 20 inclusive?
A{ price: { $gte: 10, $lte: 20 } }
B{ price: { $lt: 10, $gt: 20 } }
C{ price: { $gt: 10, $lt: 20 } }
D{ price: { $eq: 10, $eq: 20 } }
✗ Incorrect
$gte means greater than or equal, $lte means less than or equal, so this finds prices between 10 and 20 inclusive.
Describe how to combine logical operators with comparison operators in a MongoDB query.
Think about how you can ask for multiple conditions at once.
You got /4 concepts.
Explain the difference between $and and $or operators in MongoDB with examples.
Think about 'all' versus 'any' conditions.
You got /4 concepts.
Practice
(1/5)
1. Which MongoDB operator is used to combine multiple conditions where all must be true?
easy
A. $gt
B. $and
C. $or
D. $lt
Solution
Step 1: Understand logical operators
$and requires all conditions inside it to be true for a document to match.
Step 2: Compare with other operators
$or requires any condition to be true, $gt and $lt are comparison operators, not logical combiners.
Final Answer:
$and -> Option B
Quick Check:
All conditions true = $and [OK]
Hint: Use $and to require all conditions true [OK]
Common Mistakes:
Confusing $and with $or
Using comparison operators as logical
Mixing $gt with $and
2. Which of the following is the correct syntax using $and to find documents where age is greater than 25 and status is 'active'?
Documents match if age is less than 25 OR status is 'active'.
Step 2: Check each document
First doc: age 30 (not <25), status 'active' (matches). Second doc: age 20 (<25), status 'inactive' (matches age condition). Third doc: age 40 (not <25), status 'active' (matches).
Final Answer:
All three documents match -> Option D
Quick Check:
$or matches any condition true [OK]
Hint: Any condition true in $or returns document [OK]
Common Mistakes:
Ignoring one condition in $or
Confusing $or with $and
Wrongly excluding documents
4. Identify the error in this MongoDB query to find documents where score is greater than 50 and less than 100: { $and: { score: { $gt: 50 }, score: { $lt: 100 } } }
medium
A. Using object instead of array inside $and
B. Using $gt and $lt together is invalid
C. Missing $or operator
D. score field should be inside $or
Solution
Step 1: Check $and operator usage
$and requires an array of condition objects, not a single object with repeated keys.
Step 2: Understand object key overwrite
In the object, 'score' key appears twice, so only the last one is used, causing incorrect query.
Final Answer:
Using object instead of array inside $and -> Option A
Quick Check:
$and needs array, not object with duplicate keys [OK]
Hint: Use array for $and conditions to avoid key overwrite [OK]
Common Mistakes:
Using object with duplicate keys in $and
Confusing $and with $or
Thinking $gt and $lt can't combine
5. You want to find documents where (age is greater than 30 and status is 'active') or (score is less than 50). Which query correctly combines these conditions?
We want documents matching either (age > 30 AND status = 'active') OR (score < 50).
Step 2: Match query structure
{ $or: [ { $and: [ { age: { $gt: 30 } }, { status: 'active' } ] }, { score: { $lt: 50 } } ] } uses $or with two parts: one $and array for age and status, and one condition for score less than 50, matching the requirement exactly.