$not — MongoDB Technical Interview Questions & Answers
Negating a query condition.
$not
- Simple meaning: $not flips a condition. Often used with $gt or $regex.
- How to use: db.students.find({ marks: { $not: { $gt: 40 } } }) — marks that are not greater than 40 (includes missing field behaviour — check docs if you need “must exist”).
- Prefer $ne or $lte when it is clearer: { marks: { $lte: 40 } }.
- Common mistake: $not with a plain value. $not wraps an operator document.
- Interview tip: Prefer $ne/$lte for simple cases. $not for “does not match this operator”.
Technical Interview Tip: Use $not only when $ne/$lte is not enough.
Code Examples for $not
$not — working example
db.users.find({
age: { $not: { $lt: 18 } }
})$not negates the operator expression — useful when there is no direct opposite.
How to Explain $not to an Interviewer
When $not?
To negate another operator Often $ne is clearer