Introduction
MongoDB comparison operators help you find documents by comparing field values. They are especially useful when you need records where a value is greater than, less than, equal to, or different from another value. In this chapter, you will practice MongoDB comparison operators such as $eq, $ne, $gt, $gte, $lt, $lte, $in, and $nin using simple, practical queries. MongoDB Comparison Operators Practice Questions with Solutions to help you understand the concepts.
Q1. Find Students Older Than 15
Problem Statement
Find all students whose age is greater than 15.
MongoDB Command / Query
db.students.find({
age: { $gt: 15 }
})
Expected Output
[
{ name: "Rahul", age: 16, course: "Python" },
{ name: "Aman", age: 18, course: "Java" }
]
Explanation
$gt means greater than.
The query returns students where:
age > 15
Q2. Find Students Aged 16 or Older
Problem Statement
Find all students whose age is greater than or equal to 16.
MongoDB Command / Query
db.students.find({
age: { $gte: 16 }
})
Expected Output
[
{ name: "Rahul", age: 16, course: "Python" },
{ name: "Aman", age: 18, course: "Java" }
]
Explanation
$gte means greater than or equal to.
So the query finds:
age >= 16
Q3. Find Students Younger Than 18
Problem Statement
Find all students whose age is less than 18.
MongoDB Command / Query
db.students.find({
age: { $lt: 18 }
})
Expected Output
[
{ name: "Rahul", age: 16, course: "Python" },
{ name: "Priya", age: 15, course: "JavaScript" }
]
Explanation
$lt means less than.
The query searches for:
age < 18
Q4. Find Students Aged 17 or Younger
Problem Statement
Find all students whose age is less than or equal to 17.
MongoDB Command / Query
db.students.find({
age: { $lte: 17 }
})
Expected Output
[
{ name: "Rahul", age: 16, course: "Python" },
{ name: "Priya", age: 15, course: "JavaScript" }
]
Explanation
$lte means less than or equal to.
So MongoDB finds:
age <= 17
Q5. Find Students Exactly 16 Years Old
Problem Statement
Find all students whose age is exactly 16.
MongoDB Command / Query
db.students.find({
age: { $eq: 16 }
})
Expected Output
[
{ name: "Rahul", age: 16, course: "Python" }
]
Explanation
$eq means equal to.
This query finds documents where:
age = 16
For simple equality, you can also write:
db.students.find({ age: 16 })
Q6. Find Students Whose Age Is Not 16
Problem Statement
Find all students whose age is not equal to 16.
MongoDB Command / Query
db.students.find({
age: { $ne: 16 }
})
Expected Output
[
{ name: "Priya", age: 15, course: "JavaScript" },
{ name: "Aman", age: 18, course: "Java" }
]
Explanation
$ne means not equal to.
The query excludes students whose age is 16.
Q7. Find Students Taking Python or Java
Problem Statement
Find students whose course is either Python or Java.
MongoDB Command / Query
db.students.find({
course: { $in: ["Python", "Java"] }
})
Expected Output
[
{ name: "Rahul", age: 16, course: "Python" },
{ name: "Aman", age: 18, course: "Java" }
]
Explanation
$in checks whether a field matches any value from a specified list.
Here MongoDB searches for:
course = Python
OR
course = Java
Q8. Find Students Not Taking Python or Java
Problem Statement
Find students whose course is neither Python nor Java.
MongoDB Command / Query
db.students.find({
course: { $nin: ["Python", "Java"] }
})
Expected Output
[
{ name: "Priya", age: 15, course: "JavaScript" },
{ name: "Neha", age: 17, course: "C++" }
]
Explanation
$nin means not in.
It excludes documents where course is "Python" or "Java".
Q9. Find Students Between Age 15 and 17
Problem Statement
Find students whose age is between 15 and 17, including both 15 and 17.
MongoDB Command / Query
db.students.find({
age: {
$gte: 15,
$lte: 17
}
})
Expected Output
[
{ name: "Priya", age: 15, course: "JavaScript" },
{ name: "Rahul", age: 16, course: "Python" },
{ name: "Neha", age: 17, course: "C++" }
]
Explanation
Two comparison operators are used:
$gte: 15→ age must be 15 or greater$lte: 17→ age must be 17 or less
Therefore:
15 <= age <= 17
Q10. Find Students Older Than 15 and Not Taking Java
Problem Statement
Find students whose age is greater than 15 and whose course is not Java.
MongoDB Command / Query
db.students.find({
age: { $gt: 15 },
course: { $ne: "Java" }
})
Expected Output
[
{ name: "Rahul", age: 16, course: "Python" },
{ name: "Neha", age: 17, course: "C++" }
]
Explanation
This query applies two conditions:
age > 15
AND
course != Java
Both conditions must be satisfied for a document to be returned.
Key Takeaways
$eqmeans equal to.$nemeans not equal to.$gtmeans greater than.$gtemeans greater than or equal to.$ltmeans less than.$ltemeans less than or equal to.$inmatches any value from a specified list.$ninexcludes values from a specified list.- Multiple comparison operators can be used together.
- Comparison operators are commonly used with
find(),updateMany(), anddeleteMany()filters.
FAQs
1. What are comparison operators in MongoDB?
MongoDB comparison operators are query operators used to compare field values with specified values. Examples include $gt, $gte, $lt, $lte, $eq, and $ne.
2. What does $gt mean in MongoDB?
$gt means greater than.
db.students.find({ age: { $gt: 18 } })
This finds students whose age is greater than 18.
3. What is the difference between $gt and $gte?
$gt means greater than, while $gte means greater than or equal to.
$gt → age > 18
$gte → age >= 18
4. What is the difference between $lt and $lte?
$lt means less than, while $lte means less than or equal to.
$lt → age < 18
$lte → age <= 18
5. What is the use of $in in MongoDB?
$in is used when you want to match a field against multiple possible values.
db.students.find({
course: { $in: ["Python", "Java"] }
})
6. What is the use of $nin in MongoDB?
$nin means not in. It finds documents whose field value does not match any value in the specified list.
db.students.find({
course: { $nin: ["Python", "Java"] }
})
7. Can multiple comparison operators be used in one MongoDB query?
Yes. You can combine multiple comparison operators to create a more specific filter.
db.students.find({
age: { $gte: 15, $lte: 17 }
})
This finds students whose age is between 15 and 17, including both values.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
