Introduction
MongoDB Query Operators make it possible to search documents using conditions instead of only exact value matching. Operators such as $gt, $lt, $in, $ne, $and, $or, and $exists help you build flexible queries. In this chapter, you will practice the most useful MongoDB query operators with simple, real-world examples so you can understand when and how to use each operator. MongoDB Query Operators practice questions with solutions to help you understand the concepts.
Q1. Find documents using the $gt operator
Problem Statement
Find all students whose age is greater than 15.
MongoDB Command / Query
db.students.find({
age: {
$gt: 15
}
})
Expected Output
[
{
_id: ObjectId('...'),
name: "Priya",
age: 16,
course: "JavaScript"
},
{
_id: ObjectId('...'),
name: "Aman",
age: 17,
course: "MongoDB"
}
]
Explanation
$gt means greater than.
age: { $gt: 15 }
matches documents where age is greater than 15.
Q2. Find documents using the $gte operator
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
[
{
_id: ObjectId('...'),
name: "Priya",
age: 16,
course: "JavaScript"
},
{
_id: ObjectId('...'),
name: "Aman",
age: 17,
course: "MongoDB"
}
]
Explanation
$gte means greater than or equal to.
Therefore, both 16 and values above 16 will match.
Q3. Find documents using the $lt operator
Problem Statement
Find all students whose age is less than 16.
MongoDB Command / Query
db.students.find({
age: {
$lt: 16
}
})
Expected Output
[
{
_id: ObjectId('...'),
name: "Rahul",
age: 15,
course: "Python"
}
]
Explanation
$lt means less than.
age: { $lt: 16 }
returns documents where the age is below 16.
Q4. Find documents using the $lte operator
Problem Statement
Find all students whose age is less than or equal to 16.
MongoDB Command / Query
db.students.find({
age: {
$lte: 16
}
})
Expected Output
[
{
_id: ObjectId('...'),
name: "Rahul",
age: 15,
course: "Python"
},
{
_id: ObjectId('...'),
name: "Priya",
age: 16,
course: "JavaScript"
}
]
Explanation
$lte means less than or equal to.
So documents containing 15, 16, or any lower value can match this query.
Q5. Find documents using the $in operator
Problem Statement
Find students whose course is either "Python" or "MongoDB".
MongoDB Command / Query
db.students.find({
course: {
$in: ["Python", "MongoDB"]
}
})
Expected Output
[
{
_id: ObjectId('...'),
name: "Rahul",
age: 15,
course: "Python"
},
{
_id: ObjectId('...'),
name: "Aman",
age: 17,
course: "MongoDB"
}
]
Explanation
$in matches a field when its value is equal to any value in the specified array.
This is useful when you want to search for several possible values.
Q6. Find documents using the $nin operator
Problem Statement
Find students whose course is not "Python" or "MongoDB".
MongoDB Command / Query
db.students.find({
course: {
$nin: ["Python", "MongoDB"]
}
})
Expected Output
[
{
_id: ObjectId('...'),
name: "Priya",
age: 16,
course: "JavaScript"
}
]
Explanation
$nin means not in.
It returns documents where the field value does not match any value listed in the array.
Q7. Find documents using the $ne operator
Problem Statement
Find all students whose age is not equal to 15.
MongoDB Command / Query
db.students.find({
age: {
$ne: 15
}
})
Expected Output
[
{
_id: ObjectId('...'),
name: "Priya",
age: 16,
course: "JavaScript"
},
{
_id: ObjectId('...'),
name: "Aman",
age: 17,
course: "MongoDB"
}
]
Explanation
$ne means not equal to.
age: { $ne: 15 }
matches documents where age is not 15.
Q8. Find documents using the $and operator
Problem Statement
Find students whose age is greater than 14 and whose course is "Python".
MongoDB Command / Query
db.students.find({
$and: [
{
age: {
$gt: 14
}
},
{
course: "Python"
}
]
})
Expected Output
[
{
_id: ObjectId('...'),
name: "Rahul",
age: 15,
course: "Python"
}
]
Explanation
$and requires all specified conditions to be true.
The query checks:
age > 14
AND
course = Python
MongoDB also allows many simple AND conditions to be written directly:
db.students.find({
age: { $gt: 14 },
course: "Python"
})
Q9. Find documents using the $or operator
Problem Statement
Find students whose age is 15 or whose course is "MongoDB".
MongoDB Command / Query
db.students.find({
$or: [
{
age: 15
},
{
course: "MongoDB"
}
]
})
Expected Output
[
{
_id: ObjectId('...'),
name: "Rahul",
age: 15,
course: "Python"
},
{
_id: ObjectId('...'),
name: "Aman",
age: 17,
course: "MongoDB"
}
]
Explanation
$or matches a document when at least one of the conditions is true.
Here:
age = 15
OR
course = MongoDB
Q10. Find documents using the $exists operator
Problem Statement
Find all students who have a phone field in their document.
MongoDB Command / Query
db.students.find({
phone: {
$exists: true
}
})
Expected Output
[
{
_id: ObjectId('...'),
name: "Neha",
age: 16,
course: "MongoDB",
phone: "9876543210"
}
]
Explanation
$exists checks whether a field exists in a document.
$exists: true
means the phone field must exist.
To find documents where the field does not exist:
db.students.find({
phone: {
$exists: false
}
})
Key Takeaways
- MongoDB query operators allow you to create flexible search conditions.
$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.$nemeans not equal to.$andrequires all specified conditions to match.$orrequires at least one condition to match.$existschecks whether a field exists in a document.- Query operators are commonly used with
find()andfindOne().
FAQs
1. What are query operators in MongoDB?
Query operators are special MongoDB operators that allow you to search documents based on conditions. Examples include $gt, $lt, $in, $ne, $and, and $or.
2. What does $gt mean in MongoDB?
$gt means greater than.
db.students.find({
age: { $gt: 15 }
})
This finds students whose age is greater than 15.
3. What is the difference between $gt and $gte?
$gt means greater than, while $gte means greater than or equal to.
$gt → >
$gte → >=
For example, $gte: 16 includes documents where the value is exactly 16.
4. What is the difference between $in and $nin?
$in finds documents whose field matches at least one value in the specified list.
$nin finds documents whose field does not match the values in the specified list.
5. What is the difference between $and and $or?
$and requires all conditions to be true, while $or requires at least one condition to be true.
6. What does $ne do in MongoDB?
$ne means not equal to.
db.students.find({
age: { $ne: 15 }
})
This finds documents where the age field is not equal to 15.
7. What does $exists do in MongoDB?
$exists checks whether a particular field exists in a document.
db.students.find({
phone: { $exists: true }
})
This finds documents containing the phone field.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
