$eq and $ne — MongoDB Technical Interview Questions & Answers
Equal-to and not-equal-to comparison operators.
$eq and $ne
- Simple meaning: $eq means equal. $ne means not equal. Used inside a find filter, not as SQL WHERE.
- How to use: db.students.find({ roll: { $eq: 12 } }) is the same as db.students.find({ roll: 12 }). $ne: db.students.find({ city: { $ne: "Pune" } }).
- Try this: insert two students in different cities, then $ne on city.
- Common mistake: writing WHERE city != 'Pune' as a string in mongosh.
- Interview tip: Equality can skip $eq. Use $eq when you combine operators or want to be explicit.
Technical Interview Tip: Show { roll: 12 } and { city: { $ne: "Pune" } }.
Code Examples for $eq and $ne
$eq and $ne — working example
db.users.find({ city: { $eq: "Pune" } })
db.users.find({ role: { $ne: "admin" } })
// Shorthand for equality:
db.users.find({ city: "Pune" })$eq and $ne — $eq is explicit equality; $ne is not-equal. Plain { field: value } means $eq.
How to Explain $eq and $ne to an Interviewer
What is $ne?
Not equal db.students.find({ city: { $ne: "Pune" } })