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 D
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
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 A
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?
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.