MongoDB provides a wide range of query operators that allow you to filter, compare, and manipulate data more effectively. These are used inside the find() method.
$eq – Equal to$ne – Not equal to$gt – Greater than$gte – Greater than or equal to$lt – Less than$lte – Less than or equal to$in – Matches any value in an array$nin – Matches none of the values in an array
db.products.find({ price: { $gt: 100 } })
db.users.find({ name: { $in: ["Alice", "Bob"] } })
$and – Joins query clauses with logical AND$or – Joins clauses with logical OR$not – Inverts the effect of a query expression$nor – Joins clauses with NOR (none match)
db.users.find({
$or: [
{ age: { $lt: 18 } },
{ status: "inactive" }
]
})
$exists – Matches documents that have a specific field$type – Matches documents based on BSON type
db.users.find({ email: { $exists: true } })
db.data.find({ field: { $type: "string" } })
$regex – Regular expression match$expr – Use aggregation expressions in queries$mod – Modulo operation$text – Performs text search
db.products.find({ name: { $regex: /^A/i } })
db.sales.find({ $expr: { $gt: ["$price", "$discount"] } })
$all – Matches arrays that contain all specified elements$elemMatch – Matches documents that contain an array with at least one element matching all criteria$size – Matches arrays with a specific length
db.classes.find({ students: { $all: ["John", "Sarah"] } })
db.classes.find({ students: { $size: 3 } })
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!