Comparison Operators
Comparison operators filter documents by field values. Equality can be written as { field: value } or { field: { $eq: value } }. Range queries combine $gte and $lte on the same field.
$eq, $ne, $gt, $gte, $lt, $lte
Comparison operators filter documents by field values. Equality can be written as { field: value } or { field: { $eq: value } }. Range queries combine $gte and $lte on the same field.
Real-life example: Price filters on a shopping site — 'under ₹1000' is $lt: 1000, 'between ₹500 and ₹2000' is $gte plus $lte.
use rishtaara
// Products priced between ₹500 and ₹2000 (inclusive)
db.products.find({
price: { $gte: 500, $lte: 2000 }
})
// Students above grade 10
db.students.find({ grade: { $gt: 10 } })
// Not equal — exclude discontinued
db.products.find({ status: { $ne: "discontinued" } })
// Exact match (explicit $eq)
db.students.find({ grade: { $eq: 10 } })
// Same as: db.students.find({ grade: 10 })// Students in grades 10, 11, or 12 with marks above 75
db.students.find({
grade: { $gte: 10, $lte: 12 },
"scores.overall": { $gt: 75 }
})
// Low-stock alert — products with stock under 20
db.products.find({
stock: { $lt: 20 },
category: { $ne: "discontinued" }
})
// Orders over ₹5000 placed this year
db.orders.find({
total: { $gt: 5000 },
createdAt: { $gte: ISODate("2024-01-01T00:00:00Z") }
})$in and $nin — match lists
$in matches any value in an array. $nin matches none of the values — the negated form of $in.
Real-life example: $in is like a VIP list at a club — anyone on the list gets in. $nin is the banned list — those people are turned away.
// Products in electronics or furniture
db.products.find({
category: { $in: ["electronics", "furniture", "appliances"] }
})
// Students in grade 10 or 12 only
db.students.find({ grade: { $in: [10, 12] } })
// Exclude archived and discontinued categories
db.products.find({
category: { $nin: ["archived", "discontinued", "internal"] }
})
// Orders with status NOT cancelled or refunded
db.orders.find({
status: { $nin: ["cancelled", "refunded"] }
})// Premium students: grade 11 or 12, active, in top cities
db.students.find({
grade: { $in: [11, 12] },
active: true,
"address.city": { $in: ["Delhi", "Mumbai", "Bengaluru", "Chennai"] }
})
// Sale items in selected categories under ₹500
db.products.find({
category: { $in: ["stationery", "electronics"] },
price: { $lt: 500 },
onSale: true
})$cmp in aggregation — brief intro
$cmp compares two values and returns -1, 0, or 1 (like strcmp). It works inside aggregation expressions ($expr, $project, $addFields) — not as a top-level query operator on its own.
Real-life example: $cmp in aggregation is like a referee comparing two scores — who is higher, equal, or lower.
// Products where salePrice is less than cost (selling at a loss)
db.products.find({
$expr: { $eq: [{ $cmp: ["$salePrice", "$cost"] }, -1] }
// $cmp returns -1 when salePrice < cost
})
// Add a comparison flag in aggregation
db.products.aggregate([
{
$project: {
name: 1,
price: 1,
cost: 1,
priceVsCost: {
$switch: {
branches: [
{ case: { $eq: [{ $cmp: ["$price", "$cost"] }, 1] }, then: "above_cost" },
{ case: { $eq: [{ $cmp: ["$price", "$cost"] }, 0] }, then: "at_cost" },
{ case: { $eq: [{ $cmp: ["$price", "$cost"] }, -1] }, then: "below_cost" }
],
default: "unknown"
}
}
}
}
])Practical patterns for Rishtaara apps
Comparison operators power every product filter, grade report, and date range query in Rishtaara courses. Combine them with indexes on filtered fields for production performance.
Real-life example: A 'Students scoring 90+' report is just { 'scores.overall': { $gte: 90 } } — one operator, instant results.
- $ne cannot use an index efficiently in all cases — prefer $in with allowed values when possible.
- Range queries ($gt, $lt) on the same field can use a single index range scan.
- Date comparisons use ISODate or new Date() — always store dates as BSON Date type.
// Flash sale: electronics under ₹999, in stock
db.products.find({
category: "electronics",
price: { $lte: 999 },
stock: { $gt: 0 }
})
// Honor roll: grade 10+, overall score 85+
db.students.find({
grade: { $gte: 10 },
"scores.overall": { $gte: 85 },
active: true
})
// Recent high-value orders
db.orders.find({
total: { $gte: 10000 },
createdAt: { $gte: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) }
}).sort({ total: -1 })