Querying & Filtering
Comparison Operators
$eq, $ne, $gt, $gte, $lt, $lte, $in and $nin through practical filters, not a syntax list.
Operators hang on the field. { age: { $gt: 25 } } reads: the age field must be greater than 25. You already used $eq without writing it.
db.users.find({ age: { $gt: 25 } })
db.products.find({ price: { $gte: 1000, $lte: 5000 } }){ name: "Rahul", age: 28, ... }
{ name: "Mechanical Keyboard", price: 4500, ... }db.orders.find({ status: { $ne: "cancelled" } })db.orders.find({
status: { $in: ["completed", "shipped"] }
})
db.users.find({
city: { $nin: ["Mumbai", "Delhi"] }
})$in is the usual way to say 'this or this or this' on one field. $or is for conditions on different fields — next lesson. $eq / $ne / $gt / $gte / $lt / $lte / $in / $nin is the set you should be able to write cold.
| Operator | Keeps the document when |
|---|---|
| $eq | Field equals the value (the default) |
| $ne | Field is not equal — also matches missing fields |
| $gt / $gte | Greater than / greater than or equal |
| $lt / $lte | Less than / less than or equal |
| $in / $nin | Value is / is not in the list |
Interview question
How do you query a range on one field, and when do you use $in?
Range: { age: { $gte: 25, $lt: 40 } } — both operators on age. $in when one field may be any of several values, like status in completed or shipped. $or is for different fields.
Practice
Ranges and a combined amount + status filter.