MongoDB
Beginner
1 min read
Comparison and Logical Operators
Example
// $eq — explicit equality (shorthand is just { age: 30 })
db.users.find({ age: { $eq: 30 } })
// $gte / $lte — range query
db.users.find({ age: { $gte: 18, $lte: 65 } })
// $in — match any value in a list
db.products.find({ category: { $in: ["electronics", "computers"] } })
// $nin — exclude values
db.products.find({ status: { $nin: ["discontinued", "archived"] } })
// $and — all conditions must match (explicit form)
db.orders.find({
$and: [
{ total: { $gte: 100 } },
{ status: "shipped" }
]
})
// $or — at least one condition must match
db.users.find({
$or: [
{ age: { $lt: 18 } },
{ age: { $gt: 65 } }
]
})
// $not — negate a condition
db.products.find({ price: { $not: { $gt: 500 } } })
// Combined logical operators
db.orders.find({
status: "pending",
$or: [{ priority: "high" }, { total: { $gte: 1000 } }]
})