Introduction
MongoDB logical operators help you combine, reverse, or exclude query conditions. The four important operators in this chapter are $and, $or, $not, and $nor. They allow you to create flexible queries for real-world data filtering. In these practice questions, you will learn how to combine multiple conditions, match alternative conditions, reverse comparisons, and exclude multiple conditions using simple MongoDB examples. MongoDB $and, $or, $not, $nor practice questions with solutions to help you understand the concepts.
Q1. Find Students Who Are Older Than 15 and Study Python
Problem Statement
Find students whose age is greater than 15 and whose course is Python.
MongoDB Command / Query
db.students.find({
$and: [
{ age: { $gt: 15 } },
{ course: "Python" }
]
})
Expected Output
[
{
name: "Rahul",
age: 16,
course: "Python"
}
]
Explanation
$and returns documents where all conditions are true.
age > 15
AND
course = Python
Q2. Find Students Who Study Python or Java
Problem Statement
Find students whose course is either Python or Java.
MongoDB Command / Query
db.students.find({
$or: [
{ course: "Python" },
{ course: "Java" }
]
})
Expected Output
[
{
name: "Rahul",
age: 16,
course: "Python"
},
{
name: "Aman",
age: 18,
course: "Java"
}
]
Explanation
$or returns a document when at least one condition is true.
Q3. Find Students Whose Age Is Not Greater Than 17
Problem Statement
Find students whose age is not greater than 17.
MongoDB Command / Query
db.students.find({
age: {
$not: { $gt: 17 }
}
})
Expected Output
[
{
name: "Priya",
age: 15
},
{
name: "Rahul",
age: 16
},
{
name: "Neha",
age: 17
}
]
Explanation
$not reverses the result of another operator.
The condition:
NOT (age > 17)
means students aged 17 or below are returned.
Q4. Find Students Who Are Neither Studying Python Nor Java
Problem Statement
Find students whose course is neither Python nor Java.
MongoDB Command / Query
db.students.find({
$nor: [
{ course: "Python" },
{ course: "Java" }
]
})
Expected Output
[
{
name: "Priya",
age: 15,
course: "JavaScript"
},
{
name: "Neha",
age: 17,
course: "C++"
}
]
Explanation
$nor returns documents that fail all conditions inside the $nor array.
So the course must be neither Python nor Java.
Q5. Find Students Older Than 16 and From Delhi
Problem Statement
Find students whose age is greater than 16 and whose city is Delhi.
MongoDB Command / Query
db.students.find({
$and: [
{ age: { $gt: 16 } },
{ city: "Delhi" }
]
})
Expected Output
[
{
name: "Aman",
age: 18,
city: "Delhi"
}
]
Explanation
Both conditions must be satisfied:
age > 16
AND
city = Delhi
Q6. Find Students From Delhi or Mumbai
Problem Statement
Find students who live in either Delhi or Mumbai.
MongoDB Command / Query
db.students.find({
$or: [
{ city: "Delhi" },
{ city: "Mumbai" }
]
})
Expected Output
[
{
name: "Rahul",
city: "Delhi"
},
{
name: "Priya",
city: "Mumbai"
}
]
Explanation
Only one of the two conditions needs to be true.
Q7. Find Students Whose Age Is Not Equal to 16
Problem Statement
Find all students whose age is not 16.
MongoDB Command / Query
db.students.find({
age: {
$not: { $eq: 16 }
}
})
Expected Output
[
{
name: "Priya",
age: 15
},
{
name: "Neha",
age: 17
},
{
name: "Aman",
age: 18
}
]
Explanation
$not reverses $eq.
NOT (age = 16)
Therefore, students aged 16 are excluded.
Q8. Find Students Who Are Neither Under 16 Nor From Delhi
Problem Statement
Find students who are not younger than 16 and are not from Delhi.
MongoDB Command / Query
db.students.find({
$nor: [
{ age: { $lt: 16 } },
{ city: "Delhi" }
]
})
Expected Output
[
{
name: "Rahul",
age: 16,
city: "Mumbai"
},
{
name: "Neha",
age: 17,
city: "Pune"
}
]
Explanation
$nor means the document must fail both conditions.
NOT (age < 16)
AND
NOT (city = Delhi)
Q9. Combine $and and $or in MongoDB
Problem Statement
Find students who are older than 15 and are studying either Python or Java.
MongoDB Command / Query
db.students.find({
$and: [
{ age: { $gt: 15 } },
{
$or: [
{ course: "Python" },
{ course: "Java" }
]
}
]
})
Expected Output
[
{
name: "Rahul",
age: 16,
course: "Python"
},
{
name: "Aman",
age: 18,
course: "Java"
}
]
Explanation
This query combines two logical operators.
The student must:
- Be older than 15
- Study Python or Java
Q10. Combine $or and $not in MongoDB
Problem Statement
Find students who are either studying Python or whose age is not greater than 16.
MongoDB Command / Query
db.students.find({
$or: [
{ course: "Python" },
{ age: { $not: { $gt: 16 } } }
]
})
Expected Output
[
{
name: "Rahul",
age: 16,
course: "Python"
},
{
name: "Priya",
age: 15,
course: "JavaScript"
}
]
Explanation
The $or condition requires at least one condition to be true:
course = Python
OR
NOT (age > 16)
Key Takeaways
$andrequires all conditions to be true.$orin MongoDB requires at least one condition to be true.$notin MongoDB reverses the result of another query operator.$norin MongoDB requires all conditions inside it to be false.$andis useful for combining multiple requirements.$orin MongoDB is useful when there are multiple acceptable conditions.$notin MongoDB can reverse comparison operators such as$gtand$eq.$norin MongoDB can be used to exclude multiple conditions.- Logical operators can be nested and combined.
- Logical operators are useful in
find(), update filters, and delete filters.
FAQs
1. What are $and, $or, $not, and $nor in MongoDB?
They are MongoDB logical query operators used to combine, select, reverse, or exclude conditions.
$and→ all conditions must be true$or→ at least one condition must be true$not→ reverses a condition$nor→ all specified conditions must be false
2. What does $and do in MongoDB?
$and returns documents where every specified condition is satisfied.
db.students.find({
$and: [
{ age: { $gt: 15 } },
{ course: "Python" }
]
})
3. What does $or do in MongoDB?
$or returns documents where at least one condition is satisfied.
db.students.find({
$or: [
{ course: "Python" },
{ course: "Java" }
]
})
4. What is the use of $not in MongoDB?
$not reverses the result of another query operator.
db.students.find({
age: { $not: { $gt: 17 } }
})
This finds documents where the age is not greater than 17.
5. What does $nor mean in MongoDB?
$nor returns documents that do not satisfy any of the conditions specified inside the $nor array.
db.students.find({
$nor: [
{ course: "Python" },
{ course: "Java" }
]
})
6. Can MongoDB logical operators be combined?
Yes. Operators such as $and and $or can be nested to create more advanced queries.
db.students.find({
$and: [
{ age: { $gt: 15 } },
{
$or: [
{ course: "Python" },
{ course: "Java" }
]
}
]
})
7. What is the difference between $or and $nor?
$or returns a document when at least one condition is true.
$nor returns a document only when none of its conditions are true.
$or → Condition 1 OR Condition 2
$nor → NOT Condition 1 AND NOT Condition 2
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
