MongoDB comparison operators allow us to compare document field values with specific values. They are useful when searching for students by age, marks, course fees, employee salaries, product prices, and other information.
In this lesson, you will learn how to use
$eq, $ne, $gt,
$gte, $lt, $lte,
$in, and $nin.
Comparison operators are used inside MongoDB query filters to compare a field with a given value.
Example:
db.students.find({
age: { $gt: 20 }
})
This query finds students whose age is greater than 20.
Comparison operators are especially useful when you need to retrieve documents that satisfy numerical or value-based conditions.
Select the database:
use school
Insert sample student documents:
db.students.insertMany([
{
name: "Rahul",
age: 18,
marks: 55,
course: "ADCA",
fees: 5000
},
{
name: "Amit",
age: 20,
marks: 75,
course: "Python",
fees: 8000
},
{
name: "Priya",
age: 22,
marks: 90,
course: "Web Development",
fees: 10000
},
{
name: "Neha",
age: 21,
marks: 65,
course: "Python",
fees: 8000
},
{
name: "Ravi",
age: 24,
marks: 40,
course: "ADCA",
fees: 5000
}
])
Note: Run the insertion example only once to avoid duplicate sample documents.
The $eq operator matches documents where
a field equals the specified value.
db.students.find({
age: { $eq: 20 }
})
db.students.find({
course: { $eq: "Python" }
})
db.students.find({
marks: { $eq: 90 }
})
You can also use direct equality without writing
$eq.
db.students.find({
age: 20
})
Both forms match documents whose age is equal to 20.
The $ne operator matches documents where
a field value is not equal to the specified value.
It can also match documents where the field is missing.
db.students.find({
age: { $ne: 20 }
})
db.students.find({
course: { $ne: "ADCA" }
})
db.students.find({
marks: { $ne: 90 }
})
Use $ne when you want to exclude a particular
value from your search results.
The $gt operator matches documents where
a field value is greater than the specified value.
db.students.find({
age: { $gt: 20 }
})
db.students.find({
marks: { $gt: 70 }
})
db.students.find({
fees: { $gt: 5000 }
})
The specified value itself is not included in a greater-than comparison.
The $gte operator matches documents where
a field value is greater than or equal to the specified
value.
db.students.find({
age: { $gte: 21 }
})
db.students.find({
marks: { $gte: 75 }
})
db.students.find({
fees: { $gte: 8000 }
})
Unlike $gt, $gte includes
documents whose field value equals the specified value.
The $lt operator matches documents where
a field value is less than the specified value.
db.students.find({
age: { $lt: 21 }
})
db.students.find({
marks: { $lt: 60 }
})
db.students.find({
fees: { $lt: 8000 }
})
The specified value itself is not included in a less-than comparison.
The $lte operator matches documents where
a field value is less than or equal to the specified value.
db.students.find({
age: { $lte: 20 }
})
db.students.find({
marks: { $lte: 65 }
})
db.students.find({
fees: { $lte: 5000 }
})
The $lte operator includes documents whose
field value equals the specified value.
The $in operator matches a field against
any value in a specified array.
db.students.find({
course: {
$in: ["ADCA", "Python"]
}
})
db.students.find({
age: {
$in: [20, 22]
}
})
db.students.find({
marks: {
$in: [40, 90]
}
})
This operator is useful when you want to match one field against several possible values.
The $nin operator matches documents whose
field value is not in the specified array. It can also
match documents where the field is missing.
db.students.find({
course: {
$nin: ["ADCA", "Python"]
}
})
db.students.find({
age: {
$nin: [20, 22]
}
})
db.students.find({
marks: {
$nin: [40, 55]
}
})
Use $nin when you need to exclude multiple
values from the search results.
You can combine multiple conditions in a single query to retrieve more specific results.
db.students.find({
age: {
$gte: 20,
$lte: 22
}
})
This includes students aged 20, 21, or 22.
db.students.find({
marks: {
$gte: 60,
$lte: 90
}
})
db.students.find({
course: "Python",
marks: { $gt: 70 }
})
db.students.find({
fees: {
$gte: 5000,
$lte: 8000
}
})
When multiple conditions are specified for the same field, MongoDB applies them together.
| Operator | Meaning | Example |
|---|---|---|
$eq |
Equal to | { age: { $eq: 20 } } |
$ne |
Not equal to | { age: { $ne: 20 } } |
$gt |
Greater than | { age: { $gt: 20 } } |
$gte |
Greater than or equal to | { age: { $gte: 20 } } |
$lt |
Less than | { age: { $lt: 20 } } |
$lte |
Less than or equal to | { age: { $lte: 20 } } |
$in |
Matches any listed value | { age: { $in: [20, 22] } } |
$nin |
Excludes listed values | { age: { $nin: [20, 22] } } |
Use the school database and the
students collection to complete these tasks.
$eq.$ne.$in.$nin.1. Which operator means greater than?
2. Which operator matches any value in an array?
3. Which operator means less than or equal to?