Bird
Raised Fist0
MongoDBquery~20 mins

Why logical operators matter in MongoDB - Challenge Your Understanding

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 with age greater than 25 and city 'New York'
Given a collection users with documents containing age and city, which query returns users older than 25 who live in New York?
MongoDB
db.users.find({$and: [{age: {$gt: 25}}, {city: 'New York'}]})
Adb.users.find({age: {$gt: 25}, city: 'New York'})
Bdb.users.find({$or: [{age: {$gt: 25}}, {city: 'New York'}]})
Cdb.users.find({$and: [{age: {$gt: 25}}, {city: 'New York'}]})
Ddb.users.find({age: {$gt: 25}}).find({city: 'New York'})
Attempts:
2 left
💡 Hint
Use $and to combine conditions that both must be true.
query_result
intermediate
2:00remaining
Find documents where status is 'active' or score is above 80
Which MongoDB query returns documents where status is 'active' or score is greater than 80?
MongoDB
db.records.find({$or: [{status: 'active'}, {score: {$gt: 80}}]})
Adb.records.find({status: 'active', score: {$gt: 80}})
Bdb.records.find({$or: [{status: 'active'}, {score: {$gt: 80}}]})
Cdb.records.find({$and: [{status: 'active'}, {score: {$gt: 80}}]})
Ddb.records.find({status: {$in: ['active', {$gt: 80}]}})
Attempts:
2 left
💡 Hint
Use $or to match either condition.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this MongoDB query
Which option contains a syntax error in the MongoDB query to find documents where type is 'admin' and active is true?
Adb.users.find({$and: [{type: 'admin'}, {active: true}]})
Bb.users.find({$and: [{type: 'admin'}, {active: true}]})
C)}]}eurt :evitca{ ,}'nimda' :epyt{[ :dna${(dnif.sresu.bd
Ddb.users.find({type: 'admin', active: true})
Attempts:
2 left
💡 Hint
Check for matching brackets and commas.
🧠 Conceptual
advanced
2:00remaining
Why use $nor instead of $not with $or?
Which statement best explains why $nor is preferred over using $not with $or in MongoDB queries?
A<code>$not</code> can only negate single conditions, so it cannot negate an <code>$or</code> with multiple conditions, but <code>$nor</code> can.
B<code>$nor</code> returns documents where none of the conditions are true, while <code>$not</code> cannot be applied to <code>$or</code> directly.
C<code>$nor</code> is deprecated and should be replaced with <code>$not</code> and <code>$or</code>.
D<code>$not</code> with <code>$or</code> is faster than <code>$nor</code> but less readable.
Attempts:
2 left
💡 Hint
Think about what each operator can negate.
optimization
expert
3:00remaining
Optimize query to find users not in city 'Boston' and age less than 30
Which MongoDB query is the most efficient and correct way to find users who are NOT in 'Boston' and have age less than 30?
Adb.users.find({$and: [{city: {$nin: ['Boston']}}, {age: {$lt: 30}}]})
Bdb.users.find({city: {$not: {$eq: 'Boston'}}, age: {$lt: 30}})
Cdb.users.find({$nor: [{city: 'Boston'}], age: {$lt: 30}})
Ddb.users.find({$and: [{city: {$ne: 'Boston'}}, {age: {$lt: 30}}]})
Attempts:
2 left
💡 Hint
Use operators that clearly express negation and combine conditions properly.

Practice

(1/5)
1. Which logical operator in MongoDB requires all conditions to be true for a document to match?
easy
A. $and
B. $or
C. $not
D. $nor

Solution

  1. Step 1: Recall MongoDB logical operators

    $and combines multiple conditions and only matches documents where every condition is true. $or matches if any condition is true, $not excludes matches, and $nor matches if none are true.
  2. Final Answer:

    $and -> Option A
  3. Quick Check:

    $and requires all true [OK]
Hint: Remember $and means all conditions must be true [OK]
Common Mistakes:
  • Confusing $and with $or
  • Thinking $not means all true
  • Mixing $nor with $and
2. Which of the following is the correct syntax to find documents where age is greater than 25 or status is "active" in MongoDB?
easy
A. { $and: [ { age: { $gt: 25 } }, { status: "active" } ] }
B. { $nor: [ { age: { $gt: 25 } }, { status: "active" } ] }
C. { $not: [ { age: { $gt: 25 } }, { status: "active" } ] }
D. { $or: [ { age: { $gt: 25 } }, { status: "active" } ] }

Solution

  1. Step 1: Identify $or operator and verify syntax

    $or matches documents where at least one condition is true, perfect for age > 25 or status = "active". The syntax with $or and array of conditions is correct; $and requires both true, $not and $nor have different meanings.
  2. Final Answer:

    { $or: [ { age: { $gt: 25 } }, { status: "active" } ] } -> Option D
  3. Quick Check:

    $or for either condition true = { $or: [ { age: { $gt: 25 } }, { status: "active" } ] } [OK]
Hint: Use $or for any condition true, syntax needs array [OK]
Common Mistakes:
  • Using $and instead of $or
  • Wrong array brackets
  • Using $not with array incorrectly
3. Given the collection documents:
{ name: "Alice", age: 30, status: "active" }
{ name: "Bob", age: 20, status: "inactive" }
{ name: "Carol", age: 25, status: "active" }
What will the query { $and: [ { age: { $gt: 20 } }, { status: "active" } ] } return?
medium
A. [{ name: "Bob", age: 20, status: "inactive" }]
B. [{ name: "Alice", age: 30, status: "active" }, { name: "Carol", age: 25, status: "active" }]
C. [{ name: "Alice", age: 30, status: "active" }]
D. []

Solution

  1. Step 1: Evaluate documents against $and conditions

    age > 20 and status = "active": Alice (30, active) matches, Carol (25, active) matches, Bob (20, inactive) does not. Both Alice and Carol returned.
  2. Final Answer:

    [{ name: "Alice", age: 30, status: "active" }, { name: "Carol", age: 25, status: "active" }] -> Option B
  3. Quick Check:

    Both age > 20 and active status = [{ name: "Alice", age: 30, status: "active" }, { name: "Carol", age: 25, status: "active" }] [OK]
Hint: Check each condition carefully for all documents [OK]
Common Mistakes:
  • Including Bob who fails conditions
  • Returning only one document
  • Confusing $and with $or results
4. You wrote this query to exclude documents where status is "inactive":
{ $not: { status: "inactive" } }
But it returns an error. What is the problem?
medium
A. $not must be inside $and
B. $not cannot be used with strings
C. $not requires a condition operator like $eq, not a direct value
D. $not only works with numeric fields

Solution

  1. Step 1: Identify $not syntax error

    $not expects a condition operator like { status: { $not: { $eq: "inactive" } } }, not { $not: { status: "inactive" } } with direct value.
  2. Final Answer:

    $not requires a condition operator like $eq, not a direct value -> Option C
  3. Quick Check:

    $not needs operator condition [OK]
Hint: Use $not with operators like $eq, not direct values [OK]
Common Mistakes:
  • Passing direct values to $not
  • Assuming $not works alone
  • Using $not with arrays incorrectly
5. You want to find documents where age is greater than 20 but NOT with status equal to "inactive". Which query correctly uses logical operators to achieve this?
hard
A. { $and: [ { age: { $gt: 20 } }, { status: { $ne: "inactive" } } ] }
B. { $and: [ { age: { $gt: 20 } }, { $not: { status: { $eq: "inactive" } } } ] }
C. { $or: [ { age: { $gt: 20 } }, { $not: { status: { $eq: "inactive" } } } ] }
D. { $and: [ { age: { $gt: 20 } }, { $not: { status: "inactive" } } ] }

Solution

  1. Step 1: Analyze requirement and options

    Need age > 20 AND status != "inactive". A: $not with direct value (invalid). B: $not wrongly placed at top-level; must be { status: { $not: { $eq: "inactive" } } } (but $ne simpler). C: $or allows documents failing one condition. D: Correctly uses $and with $gt and $ne.
  2. Final Answer:

    { $and: [ { age: { $gt: 20 } }, { status: { $ne: "inactive" } } ] } -> Option A
  3. Quick Check:

    Use $ne for NOT equal, combine with $and = { $and: [ { age: { $gt: 20 } }, { status: { $ne: "inactive" } } ] } [OK]
Hint: Use $ne for NOT equal, combine with $and for all true [OK]
Common Mistakes:
  • Using $not with direct value
  • Using $or instead of $and
  • Overcomplicating with $not and $eq