Matching an exact value only gets you so far. Query operators — always written with a leading $ — let you filter by ranges, sets, and combinations of conditions.
| Operator | Meaning |
|---|---|
| $gt | Greater than |
| $gte | Greater than or equal to |
| $lt | Less than |
| $lte | Less than or equal to |
| $ne | Not equal to |
| $in | Matches any value in a given array |
| $nin | Matches none of the values in a given array |
// Users older than 25
db.users.find({ age: { $gt: 25 } })
// Users whose role is one of these three
db.users.find({ role: { $in: ["admin", "editor", "moderator"] } })$and and $or combine multiple conditions. In practice, listing two fields in the same object is already an implicit $and — the explicit operator is mainly needed when combining multiple conditions on the same field.
// Implicit AND — age > 20 AND role = "editor"
db.users.find({ age: { $gt: 20 }, role: "editor" })
// Explicit OR — role is admin OR age is over 60
db.users.find({
$or: [
{ role: "admin" },
{ age: { $gt: 60 } }
]
})$exists checks whether a field is present at all — genuinely useful in MongoDB, since not every document is guaranteed to have every field:
db.users.find({ phoneNumber: { $exists: true } })Operators combine freely
All of these operators combine freely inside one query — you are not limited to using just one at a time. A real query filtering "active editors aged 20–35" would use $gte, $lte, and an implicit $and together in a single object.