Hint: Check each document's field against operator condition [OK]
Common Mistakes:
Including documents that do not meet the condition
Confusing $gt with $lt
Assuming all documents are returned
4. What is wrong with this MongoDB query to find users younger than 40?
db.users.find({ age: $lt: 40 })
medium
A. The $lt operator should be replaced with $gt
B. The operator $lt should be inside curly braces after the field name
C. The query should use parentheses instead of curly braces
D. The field name should be prefixed with $
Solution
Step 1: Analyze operator placement
The operator $lt must be inside an object as the value of the field key.
Step 2: Correct syntax structure
The correct syntax is { age: { $lt: 40 } }. The given query misses the curly braces around the operator.
Final Answer:
The operator $lt should be inside curly braces after the field name -> Option B
Quick Check:
Operators need braces inside field object = A [OK]
Hint: Always wrap operators in braces inside the field object [OK]
Common Mistakes:
Writing operator outside braces
Using wrong operator for condition
Misplacing $ before field name
5. You want to find all products priced between 50 and 100 inclusive in a MongoDB collection. Which query correctly uses query operators to achieve this?
hard
A. { price: { $gte: 50, $lte: 100 } }
B. { price: { $gt: 50, $lt: 100 } }
C. { price: { $gte: 50 }, { $lte: 100 } }
D. { price: { $between: [50, 100] } }
Solution
Step 1: Understand inclusive range operators
To include 50 and 100, use $gte (greater or equal) and $lte (less or equal).
Step 2: Check correct syntax for multiple operators
Both operators must be inside the same object for the field: { price: { $gte: 50, $lte: 100 } }.
Final Answer:
{ price: { $gte: 50, $lte: 100 } } -> Option A
Quick Check:
Inclusive range uses $gte and $lte together = B [OK]
Hint: Use $gte and $lte inside one object for inclusive ranges [OK]