The query { age: { $ne: 25 } } finds documents where the age is NOT equal to 25.
Step 2: Check each document's age
Alice and Carol have age 25, so they are excluded. Bob has age 30, so he matches.
Final Answer:
[{ "name": "Bob", "age": 30 }] -> Option A
Quick Check:
Only age != 25 returns Bob [OK]
Hint: Exclude matching values with $ne to get others [OK]
Common Mistakes:
Including documents with age 25
Returning all documents by mistake
Confusing $ne with $eq
4.
Identify the error in this MongoDB query to find documents where category is NOT equal to "books":
db.collection.find({ category: { $ne: books } })
medium
A. The query is correct as is
B. Incorrect operator, should use $neq instead of $ne
C. The field name should be in quotes
D. Missing quotes around the string value "books"
Solution
Step 1: Check the value type in the query
The value "books" is a string and must be enclosed in quotes in MongoDB queries.
Step 2: Verify operator and field name
The operator $ne is correct, and field names do not require quotes unless special characters are present.
Final Answer:
Missing quotes around the string value "books" -> Option D
Quick Check:
String values need quotes [OK]
Hint: Always quote string values in queries [OK]
Common Mistakes:
Leaving string values unquoted
Using $neq instead of $ne
Quoting field names unnecessarily
5.
You have a collection products with documents containing type and price. You want to find all products that are NOT of type "electronics" and have a price NOT equal to 100. Which query correctly uses $ne to achieve this?