MongoDB query operators help us search for documents based on specific conditions. They allow us to compare values, check whether fields exist, match values in arrays, and perform more advanced searches.
In this lesson, we will learn commonly used query operators with practical examples using a student management database.
Query operators are special MongoDB operators used inside query filters to specify conditions for retrieving documents.
Example:
db.students.find({
age: { $gt: 20 }
})
In this example, $gt means greater than.
MongoDB returns students whose age is greater than 20.
Query operators are commonly grouped into categories:
Select the database:
use school
Insert sample student documents:
db.students.insertMany([
{
name: "Rahul",
age: 20,
marks: 75,
course: "ADCA",
city: "Patna",
skills: ["HTML", "CSS"],
active: true
},
{
name: "Amit",
age: 22,
marks: 88,
course: "Python",
city: "Gaya",
skills: ["Python", "SQL"],
active: true
},
{
name: "Priya",
age: 19,
marks: 92,
course: "Web Development",
city: "Patna",
skills: ["HTML", "JavaScript"],
active: false
},
{
name: "Neha",
age: 21,
marks: 65,
course: "Python",
city: "Aurangabad",
skills: ["Python", "HTML"],
active: true
},
{
name: "Ravi",
age: 23,
marks: 55,
course: "ADCA",
city: "Gaya",
skills: ["Excel", "Tally"],
active: false
}
])
Note: Run this insertion example only once to avoid creating duplicate sample records.
Comparison operators compare field values with specified values.
| Operator | Meaning |
|---|---|
$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 |
Does not match values in an array |
db.students.find({
age: { $eq: 20 }
})
db.students.find({
course: { $ne: "ADCA" }
})
db.students.find({
marks: { $gt: 80 }
})
db.students.find({
age: { $gte: 21 }
})
db.students.find({
marks: { $lt: 70 }
})
db.students.find({
age: { $lte: 20 }
})
Use $in to match a field against any value
in a specified array.
db.students.find({
course: {
$in: ["ADCA", "Python"]
}
})
This query returns students whose course is either ADCA or Python.
db.students.find({
course: {
$nin: ["ADCA", "Python"]
}
})
This query returns documents whose course is not one
of the specified values. For fields that are missing,
$nin can also match the document.
Logical operators combine or reverse query conditions.
| Operator | Meaning |
|---|---|
$and |
All conditions must match. |
$or |
At least one condition must match. |
$nor |
None of the conditions may match. |
$not |
Negates a field query condition. |
db.students.find({
$and: [
{ age: { $gt: 20 } },
{ marks: { $gt: 70 } }
]
})
db.students.find({
$or: [
{ course: "ADCA" },
{ city: "Patna" }
]
})
db.students.find({
$nor: [
{ city: "Patna" },
{ course: "Python" }
]
})
db.students.find({
marks: {
$not: { $gt: 80 }
}
})
This last query matches documents where marks do not satisfy the greater-than-80 condition. Depending on the field and operator, missing fields can also match a negated query.
Element operators check whether a field exists and whether its value has a particular BSON type.
db.students.find({
marks: { $exists: true }
})
This finds documents that contain the
marks field.
db.students.find({
phone: { $exists: false }
})
This finds documents where the phone
field is missing.
db.students.find({
age: { $type: "number" }
})
The $type operator lets you search for
fields of a specified BSON type. The type name must
be supported by MongoDB.
Array operators allow you to search documents based on the values stored in array fields.
db.students.find({
skills: "HTML"
})
This matches documents where the skills
array contains HTML.
db.students.find({
skills: {
$all: ["HTML", "CSS"]
}
})
The array must contain both HTML and CSS, though the values may appear in any order.
db.students.find({
skills: {
$size: 2
}
})
This matches arrays containing exactly two elements.
Evaluation operators support searches such as regular expression matching and certain expression-based conditions.
db.students.find({
name: { $regex: "^R" }
})
The ^R pattern matches names beginning
with the letter R.
db.students.find({
name: {
$regex: "^r",
$options: "i"
}
})
The i option makes the regular expression
case-insensitive.
db.students.find({
$expr: {
$gt: ["$marks", 80]
}
})
The $expr operator allows aggregation
expressions to be used inside a query filter.
You can combine multiple query conditions to build more specific searches.
db.students.find({
age: { $gt: 20 },
active: true
})
db.students.find({
course: "Python",
marks: { $gte: 70 }
})
db.students.find({
city: { $in: ["Patna", "Gaya"] },
marks: { $gt: 60 }
})
db.students.find({
active: true,
skills: "HTML"
})
| Operator | Purpose |
|---|---|
$gt |
Greater than |
$gte |
Greater than or equal to |
$lt |
Less than |
$lte |
Less than or equal to |
$eq |
Equal to |
$ne |
Not equal to |
$in |
Matches any listed value |
$nin |
Does not match listed values |
$and |
All conditions must match |
$or |
At least one condition must match |
$exists |
Checks field existence |
$type |
Matches a BSON data type |
$all |
Matches all specified array elements |
$size |
Matches an array by its size |
$regex |
Matches a regular expression pattern |
Use the school database and the sample
students collection to complete these tasks.
$in.$ne.$or.$regex.1. Which operator means greater than?
2. Which operator matches at least one condition?
3. Which operator checks whether a field exists?