Comparison Operators
Learn how to filter documents in MongoDB using comparison operators — $eq, $ne, $gt, $gte, $lt, $lte, $in, and $nin.
Comparison Operators
When you query MongoDB, you often need more than an exact match. You want documents where a field is greater than a value, or not equal to something, or in a list of values. That is what comparison operators are for.
All comparison operators start with a $ sign. You use them inside a query filter like this:
db.collection.find({ field: { $operator: value } })$eq — Equal
$eq matches documents where a field equals a specific value. This is the default behavior when you write a plain filter — you rarely need to write $eq explicitly.
// These two queries do exactly the same thing
db.students.find({ grade: "10th" })
db.students.find({ grade: { $eq: "10th" } })Use $eq explicitly when you need it inside a more complex expression — like with $expr. Otherwise the shorthand is cleaner.
$ne — Not Equal
$ne matches documents where a field does not equal a specific value.
// Find all students who are NOT in 10th grade
db.students.find({ grade: { $ne: "10th" } })// Find all teachers who are not teaching Math
db.teachers.find({ subject: { $ne: "Math" } })$gt — Greater Than
$gt matches documents where a field is greater than a value.
// Find students older than 16
db.students.find({ age: { $gt: 16 } })This returns students where age is 17, 18, 19 — but not 16 itself.
$gte — Greater Than or Equal
$gte matches documents where a field is greater than or equal to a value.
// Find students aged 16 or older
db.students.find({ age: { $gte: 16 } })This returns students where age is 16, 17, 18 — including 16 itself.
$lt — Less Than
$lt matches documents where a field is less than a value.
// Find students younger than 16
db.students.find({ age: { $lt: 16 } })This returns students where age is 15, 14, 13 — but not 16 itself.
$lte — Less Than or Equal
$lte matches documents where a field is less than or equal to a value.
// Find students aged 16 or younger
db.students.find({ age: { $lte: 16 } })Combining $gt and $lt for a Range
You can put multiple comparison operators on the same field to create a range query:
// Find students between 15 and 17 years old (inclusive)
db.students.find({
age: { $gte: 15, $lte: 17 }
})// Find courses with more than 20 but less than 30 students
db.courses.find({
totalStudents: { $gt: 20, $lt: 30 }
})Comparing Dates
Comparison operators work on dates too — MongoDB compares them chronologically:
// Find students enrolled after September 1st 2024
db.students.find({
enrollmentDate: { $gt: new Date("2024-09-01") }
})
// Find students enrolled between two dates
db.students.find({
enrollmentDate: {
$gte: new Date("2024-01-01"),
$lte: new Date("2024-12-31")
}
})$in — In a List
$in matches documents where a field's value is in a given array of values. It is like writing multiple $eq conditions combined with OR.
// Find students in 9th, 10th, or 11th grade
db.students.find({
grade: { $in: ["9th", "10th", "11th"] }
})// Find teachers who teach Math, Physics, or Biology
db.teachers.find({
subject: { $in: ["Math", "Physics", "Biology"] }
})$in also works with ObjectIds — useful when you have a list of IDs and want to fetch all of them at once:
const { ObjectId } = require('mongodb');
db.students.find({
_id: {
$in: [
new ObjectId("64a1f2c3e4b0a1b2c3d4e5f6"),
new ObjectId("64a1f2c3e4b0a1b2c3d4e5f7"),
new ObjectId("64a1f2c3e4b0a1b2c3d4e5f8")
]
}
})$nin — Not In a List
$nin matches documents where a field's value is not in a given array of values. It is the opposite of $in.
// Find students who are NOT in 9th or 10th grade
db.students.find({
grade: { $nin: ["9th", "10th"] }
})// Find all courses except Math and Physics
db.courses.find({
title: { $nin: ["Mathematics", "Physics"] }
})All Comparison Operators at a Glance
| Operator | Meaning | Example |
|---|---|---|
$eq | Equal to | { age: { $eq: 16 } } |
$ne | Not equal to | { age: { $ne: 16 } } |
$gt | Greater than | { age: { $gt: 16 } } |
$gte | Greater than or equal | { age: { $gte: 16 } } |
$lt | Less than | { age: { $lt: 16 } } |
$lte | Less than or equal | { age: { $lte: 16 } } |
$in | Value is in a list | { grade: { $in: ["9th", "10th"] } } |
$nin | Value is not in a list | { grade: { $nin: ["9th", "10th"] } } |
School System Examples
Here are practical queries for our school system combining multiple comparison operators:
// Students who are 15 or 16 years old
db.students.find({ age: { $in: [15, 16] } })
// Students older than 15 and enrolled
db.students.find({
age: { $gt: 15 },
enrolled: true
})
// Courses with at least 25 students
db.courses.find({ totalStudents: { $gte: 25 } })
// Teachers who joined before 2018
db.teachers.find({
joinedDate: { $lt: new Date("2018-01-01") }
})
// Students not in 9th or 12th grade
db.students.find({
grade: { $nin: ["9th", "12th"] }
})
// Courses with between 20 and 30 students
db.courses.find({
totalStudents: { $gte: 20, $lte: 30 }
})When you need to check if a field equals one of several values, always use $in instead of writing multiple separate queries. One $in query is faster and cleaner than running the same query five times with different values.