We combine logical and comparison operators to find data that meets multiple conditions at once. This helps us get exactly the information we want.
Combining logical and comparison operators in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
MongoDB
{ $and: [ { field1: { $operator: value } }, { field2: { $operator: value } } ] }
{ $or: [ { field1: { $operator: value } }, { field2: { $operator: value } } ] }$and means all conditions must be true.
$or means at least one condition must be true.
Examples
MongoDB
{ $and: [ { age: { $gt: 18 } }, { city: "New York" } ] }MongoDB
{ $or: [ { price: { $lt: 50 } }, { onSale: true } ] }MongoDB
{ $and: [ { pages: { $gt: 300 } }, { author: "Jane Doe" } ] }Sample Program
This query finds all products in the 'electronics' category that cost less than 100.
MongoDB
db.products.find({
$and: [
{ price: { $lt: 100 } },
{ category: "electronics" }
]
})Important Notes
You can combine many conditions inside $and or $or arrays.
If you only use $and, you can also write conditions directly without $and, because MongoDB treats multiple conditions as AND by default.
Logical operators help you build flexible and precise queries.
Summary
Logical operators like $and and $or combine multiple conditions.
Comparison operators like $gt (greater than) and $lt (less than) check values.
Use them together to find exactly the data you want.
Practice
1. Which MongoDB operator is used to combine multiple conditions where all must be true?
easy
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 BQuick 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'?
easy
Solution
Step 1: Check $and syntax
$and requires an array of conditions, so the value must be an array of objects.Step 2: Validate other options
{ $or: [ { age: { $gt: 25 } }, { status: 'active' } ] } uses $or, { age: { $gt: 25 }, status: 'active' } uses implicit AND but not with $and operator, { $and: { age: { $gt: 25 }, status: 'active' } } uses $and with an object instead of array which is invalid.Final Answer:
{ $and: [ { age: { $gt: 25 } }, { status: 'active' } ] } -> Option AQuick Check:
$and needs array of conditions [OK]
Hint: Use array inside $and for multiple conditions [OK]
Common Mistakes:
- Using object instead of array in $and
- Confusing $and with $or
- Missing $gt operator syntax
3. Given the collection documents: [{ age: 30, status: 'active' }, { age: 20, status: 'inactive' }, { age: 40, status: 'active' }], what documents match this query?
{ $or: [ { age: { $lt: 25 } }, { status: 'active' } ] }medium
Solution
Step 1: Understand $or condition
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 DQuick 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
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 AQuick 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?
hard
Solution
Step 1: Break down the condition
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.Final Answer:
{ $or: [ { $and: [ { age: { $gt: 30 } }, { status: 'active' } ] }, { score: { $lt: 50 } } ] } -> Option CQuick Check:
Nested $and inside $or matches complex logic [OK]
Hint: Use nested $and inside $or for combined conditions [OK]
Common Mistakes:
- Using $and instead of $or for main condition
- Wrong object instead of array syntax
- Mixing $or and $and incorrectly
