Bird
Raised Fist0
MongoDBquery~20 mins

Combining 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
🎖️
Master of Combining Comparison Operators
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents with age between 25 and 35 inclusive
Given a collection users with documents containing an age field, which query returns all users whose age is between 25 and 35 inclusive?
MongoDB
db.users.find({ age: { $gte: 25, $lte: 35 } })
Adb.users.find({ age: { $gte: 25 }, age: { $lte: 35 } })
Bdb.users.find({ age: { $gt: 25, $lt: 35 } })
Cdb.users.find({ age: { $gte: 25, $lte: 35 } })
Ddb.users.find({ age: { $gte: 25 } }, { age: { $lte: 35 } })
Attempts:
2 left
💡 Hint
Use $gte and $lte together inside the same object for the field.
query_result
intermediate
2:00remaining
Find documents where score is less than 50 or greater than 90
Which MongoDB query returns documents where the score field is either less than 50 or greater than 90?
MongoDB
db.scores.find({ $or: [ { score: { $lt: 50 } }, { score: { $gt: 90 } } ] })
Adb.scores.find({ score: { $lt: 50, $gt: 90 } })
Bdb.scores.find({ score: { $lt: 50 } }, { score: { $gt: 90 } })
Cdb.scores.find({ $and: [ { score: { $lt: 50 } }, { score: { $gt: 90 } } ] })
Ddb.scores.find({ $or: [ { score: { $lt: 50 } }, { score: { $gt: 90 } } ] })
Attempts:
2 left
💡 Hint
Use $or to combine conditions that can be true alternatively.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this query combining comparison operators
Which option contains a syntax error when trying to find documents with price between 100 and 200?
MongoDB
db.products.find({ price: { $gte: 100, $lte: 200 } })
Adb.products.find({ price: { $gte: 100, $lte: 200 } })
Bdb.products.find({ price: { $gte: 100 }, price: { $lte: 200 } })
Cdb.products.find({ price: { $gte: 100, $lte: 200 } }, {})
Ddb.products.find({ price: { $gte: 100, $lte: 200 }, })
Attempts:
2 left
💡 Hint
Check if the same field appears twice in the same object.
optimization
advanced
2:00remaining
Optimize query to find documents with age not between 20 and 30
Which query is the most efficient and correct way to find documents where age is NOT between 20 and 30 inclusive?
Adb.users.find({ $or: [ { age: { $lt: 20 } }, { age: { $gt: 30 } } ] })
Bdb.users.find({ age: { $not: { $gte: 20, $lte: 30 } } })
Cdb.users.find({ age: { $ne: { $gte: 20, $lte: 30 } } })
Ddb.users.find({ age: { $lt: 20, $gt: 30 } })
Attempts:
2 left
💡 Hint
Use $or to combine conditions that exclude the range.
🧠 Conceptual
expert
2:00remaining
Understanding behavior of combined comparison operators in MongoDB
Consider the query db.collection.find({ value: { $gt: 10, $lt: 20 } }). Which statement about this query is true?
AIt returns documents where value is greater than 10 and less than 20.
BIt returns documents where value is greater than 10 or less than 20.
CIt returns documents where value is exactly 10 or 20.
DIt causes a syntax error because multiple operators cannot be combined.
Attempts:
2 left
💡 Hint
Think about how MongoDB interprets multiple operators inside one field object.

Practice

(1/5)
1. Which MongoDB query correctly finds documents where the field age is greater than 20 and less than 30?
easy
A. { age: { $gte: 20, $lte: 30 } }
B. { age: { $gt: 20, $lt: 30 } }
C. { age: { $ne: 20, $ne: 30 } }
D. { age: { $gt: 20 }, $lt: 30 }

Solution

  1. Step 1: Understand the requirement for age range

    The question asks for documents where age is strictly greater than 20 and strictly less than 30.
  2. Step 2: Identify the correct MongoDB syntax for combined conditions

    Using { age: { $gt: 20, $lt: 30 } } combines both conditions on the same field correctly.
  3. Final Answer:

    { age: { $gt: 20, $lt: 30 } } -> Option B
  4. Quick Check:

    Combined $gt and $lt on same field = { age: { $gt: 20, $lt: 30 } } [OK]
Hint: Use $gt and $lt together inside one field object [OK]
Common Mistakes:
  • Using $gte and $lte includes boundary values, not strictly between
  • Placing $lt outside the field object causes syntax error
  • Using $ne does not check range but excludes specific values
2. Which of the following is the correct syntax to find documents where score is not equal to 50 and less than or equal to 100?
easy
A. { score: { $ne: 50, $lte: 100 } }
B. { score: { $ne: 50 }, $lte: 100 }
C. { score: { $ne: 50, $lt: 100 } }
D. { score: { $ne: 50, $gte: 100 } }

Solution

  1. Step 1: Combine $ne and $lte inside the same field object

    Both conditions must apply to score, so they go together inside one object.
  2. Step 2: Check each option's syntax

    { score: { $ne: 50, $lte: 100 } } correctly combines $ne: 50 and $lte: 100 inside score. { score: { $ne: 50 }, $lte: 100 } separates $lte outside, which is invalid syntax.
  3. Final Answer:

    { score: { $ne: 50, $lte: 100 } } -> Option A
  4. Quick Check:

    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

  1. 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.
  2. 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.
  3. Final Answer:

    Documents where price is between 100 and 200, and stock is between 20 and 40 -> Option C
  4. 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

  1. 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.
  2. 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 } }.
  3. Final Answer:

    The $lt operator is outside the field object and causes syntax error -> Option D
  4. 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?
hard
A. { rating: { $gt: 3, $ne: 5 }, reviews: { $gte: 10, $lte: 100 } }
B. { rating: { $gt: 3 }, rating: { $ne: 5 }, reviews: { $gte: 10, $lte: 100 } }
C. { rating: { $gt: 3, $ne: 5, $gte: 10 }, reviews: { $lte: 100 } }
D. { rating: { $gt: 3, $ne: 5 }, reviews: { $gt: 10, $lt: 100 } }

Solution

  1. Step 1: Combine multiple comparison operators on the same field correctly

    Both $gt: 3 and $ne: 5 must be inside the same rating object to apply both conditions.
  2. Step 2: Use inclusive range operators for reviews

    Since reviews should be between 10 and 100 inclusive, use $gte: 10 and $lte: 100 inside the reviews object.
  3. Step 3: Check for syntax errors and logical correctness

    { rating: { $gt: 3, $ne: 5 }, reviews: { $gte: 10, $lte: 100 } } correctly combines all conditions. { rating: { $gt: 3 }, rating: { $ne: 5 }, reviews: { $gte: 10, $lte: 100 } } repeats the rating field, which is invalid. { rating: { $gt: 3, $ne: 5, $gte: 10 }, reviews: { $lte: 100 } } incorrectly adds $gte: 10 to rating. { rating: { $gt: 3, $ne: 5 }, reviews: { $gt: 10, $lt: 100 } } uses exclusive operators for reviews, which is incorrect.
  4. Final Answer:

    { rating: { $gt: 3, $ne: 5 }, reviews: { $gte: 10, $lte: 100 } } -> Option A
  5. Quick Check:

    Combine all conditions inside field objects correctly = { rating: { $gt: 3, $ne: 5 }, reviews: { $gte: 10, $lte: 100 } } [OK]
Hint: Put all conditions for one field inside one object, use correct operators [OK]
Common Mistakes:
  • Repeating the same field multiple times in query
  • Mixing inclusive and exclusive operators incorrectly
  • Adding unrelated operators inside wrong field objects