Bird
Raised Fist0
MongoDBquery~5 mins

Combining logical and comparison operators in MongoDB - Cheat Sheet & Quick Revision

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
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
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 } }
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'
Which operator would you use to combine conditions that all must be true?
A$and
B$or
C$nor
D$exists
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 } }
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

      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