Bird
Raised Fist0
MongoDBquery~20 mins

Combining logical and comparison operators in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Logical Operator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents matching combined conditions
Given a collection products with fields price and category, which query returns all products with price greater than 100 and category either 'electronics' or 'appliances'?
MongoDB
db.products.find({ $and: [ { price: { $gt: 100 } }, { category: { $in: ['electronics', 'appliances'] } } ] })
Adb.products.find({ price: { $gte: 100 }, category: { $nin: ['electronics', 'appliances'] } })
Bdb.products.find({ $or: [ { price: { $gt: 100 } }, { category: { $in: ['electronics', 'appliances'] } } ] })
Cdb.products.find({ $and: [ { price: { $lt: 100 } }, { category: { $in: ['electronics', 'appliances'] } } ] })
Ddb.products.find({ price: { $gt: 100 }, category: { $in: ['electronics', 'appliances'] } })
Attempts:
2 left
💡 Hint
Use $and to combine conditions that must both be true.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this MongoDB query
Which option contains a syntax error in combining logical and comparison operators to find users aged between 18 and 30 inclusive?
MongoDB
db.users.find({ age: { $gte: 18, $lte: 30 } })
Adb.users.find({ age: { $gte: 18, $lte: 30 } })
Bdb.users.find({ $and: [ { age: { $gte: 18 } }, { age: { $lte: 30 } } ] })
Cdb.users.find({ age: { $gte: 18 }, age: { $lte: 30 } })
Ddb.users.find({ age: { $gte: 18, $lt: 30 } })
Attempts:
2 left
💡 Hint
Check if the object keys are repeated in the same level.
optimization
advanced
2:00remaining
Optimize query combining multiple logical operators
Which query is the most efficient to find documents where status is 'active' and either score is above 80 or rank is below 10?
Adb.records.find({ $and: [ { status: 'active' }, { $or: [ { score: { $gt: 80 } }, { rank: { $lt: 10 } } ] } ] })
Bdb.records.find({ status: 'active', $or: [ { score: { $gt: 80 } }, { rank: { $lt: 10 } } ] })
Cdb.records.find({ $or: [ { status: 'active', score: { $gt: 80 } }, { status: 'active', rank: { $lt: 10 } } ] })
Ddb.records.find({ $or: [ { status: 'active' }, { score: { $gt: 80 } }, { rank: { $lt: 10 } } ] })
Attempts:
2 left
💡 Hint
Use implicit $and by combining conditions in one object when possible.
🔧 Debug
advanced
2:00remaining
Debug why this query returns no results
A query is intended to find documents where age is between 20 and 40 and verified is true, but it returns no results. Which option explains the likely cause?
MongoDB
db.users.find({ $and: [ { age: { $gt: 20, $lt: 40 } }, { verified: true } ] })
AThe query should use $gte and $lte to include ages 20 and 40; using $gt and $lt excludes boundary ages.
BThe $and operator is invalid syntax in MongoDB queries, causing no results.
CThe query uses $gt and $lt, so it excludes age 20 and 40; if no documents have age strictly between 20 and 40, no results appear.
DThe verified field should be a string 'true' not boolean true, so no matches occur.
Attempts:
2 left
💡 Hint
Check if the comparison operators exclude boundary values.
🧠 Conceptual
expert
3:00remaining
Understanding precedence of logical operators in MongoDB queries
Consider the query: db.collection.find({ $or: [ { status: 'A' }, { $and: [ { qty: { $lt: 30 } }, { price: { $gt: 20 } } ] } ] }). Which statement best describes how MongoDB evaluates this query?
AMongoDB returns documents where status is 'A' or both qty is less than 30 and price is greater than 20.
BMongoDB returns documents where status is 'A' and either qty is less than 30 or price is greater than 20.
CMongoDB returns documents where status is 'A' and qty is less than 30 and price is greater than 20.
DMongoDB returns documents where status is 'A' or qty is less than 30 or price is greater than 20.
Attempts:
2 left
💡 Hint
Recall that $and has higher precedence inside $or arrays.

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

  1. Step 1: Understand logical operators

    $and requires all conditions inside it to be true for a document to match.
  2. Step 2: Compare with other operators

    $or requires any condition to be true, $gt and $lt are comparison operators, not logical combiners.
  3. Final Answer:

    $and -> Option B
  4. 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'?
easy
A. { $and: [ { age: { $gt: 25 } }, { status: 'active' } ] }
B. { $or: [ { age: { $gt: 25 } }, { status: 'active' } ] }
C. { age: { $gt: 25 }, status: 'active' }
D. { $and: { age: { $gt: 25 }, status: 'active' } }

Solution

  1. Step 1: Check $and syntax

    $and requires an array of conditions, so the value must be an array of objects.
  2. 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.
  3. Final Answer:

    { $and: [ { age: { $gt: 25 } }, { status: 'active' } ] } -> Option A
  4. Quick 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
A. [{ age: 30, status: 'active' }, { age: 40, status: 'active' }]
B. []
C. [{ age: 20, status: 'inactive' }]
D. [{ age: 20, status: 'inactive' }, { age: 30, status: 'active' }, { age: 40, status: 'active' }]

Solution

  1. Step 1: Understand $or condition

    Documents match if age is less than 25 OR status is 'active'.
  2. 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).
  3. Final Answer:

    All three documents match -> Option D
  4. 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

  1. Step 1: Check $and operator usage

    $and requires an array of condition objects, not a single object with repeated keys.
  2. Step 2: Understand object key overwrite

    In the object, 'score' key appears twice, so only the last one is used, causing incorrect query.
  3. Final Answer:

    Using object instead of array inside $and -> Option A
  4. 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?
hard
A. { $and: { $or: [ { age: { $gt: 30 } }, { status: 'active' } ], score: { $lt: 50 } } }
B. { $and: [ { age: { $gt: 30 } }, { status: 'active' }, { score: { $lt: 50 } } ] }
C. { $or: [ { $and: [ { age: { $gt: 30 } }, { status: 'active' } ] }, { score: { $lt: 50 } } ] }
D. { $or: { age: { $gt: 30 }, status: 'active', score: { $lt: 50 } } }

Solution

  1. Step 1: Break down the condition

    We want documents matching either (age > 30 AND status = 'active') OR (score < 50).
  2. 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.
  3. Final Answer:

    { $or: [ { $and: [ { age: { $gt: 30 } }, { status: 'active' } ] }, { score: { $lt: 50 } } ] } -> Option C
  4. Quick 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