MongoDB Logical Operators Practice Questions with Solutions

Introduction

MongoDB logical operators allow you to combine multiple conditions while searching for documents. They are useful when a query needs to check more than one rule, such as finding students who are older than 15 and studying Python, or students who are studying either Java or C++. In this chapter, you will practice $and, $or, $nor, and $not with simple, practical MongoDB queries. MongoDB Logical operators 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 requires all specified conditions to be true.

Here:

age > 15
AND
course = Python

Q2. Find Students Who Are Younger Than 16 or Study Java

Problem Statement

Find students whose age is less than 16 or whose course is Java.

MongoDB Command / Query

db.students.find({
  $or: [
    { age: { $lt: 16 } },
    { course: "Java" }
  ]
})

Expected Output

[
  {
    name: "Priya",
    age: 15,
    course: "JavaScript"
  },
  {
    name: "Aman",
    age: 18,
    course: "Java"
  }
]

Explanation

$or returns a document when at least one condition is true.


Q3. Find Students Who Are Not From Python or 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 the specified conditions.

In simple words, the result should not match either:

course = Python
course = Java

Q4. 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,
    course: "JavaScript"
  },
  {
    name: "Rahul",
    age: 16,
    course: "Python"
  },
  {
    name: "Neha",
    age: 17,
    course: "C++"
  }
]

Explanation

$not reverses the result of another operator.

Here:

NOT (age > 17)

So students aged 17 or below are returned.


Q5. Find Students Between Age 15 and 18

Problem Statement

Find students whose age is between 15 and 18, including both 15 and 18.

MongoDB Command / Query

db.students.find({
  $and: [
    { age: { $gte: 15 } },
    { age: { $lte: 18 } }
  ]
})

Expected Output

[
  {
    name: "Priya",
    age: 15,
    course: "JavaScript"
  },
  {
    name: "Rahul",
    age: 16,
    course: "Python"
  },
  {
    name: "Neha",
    age: 17,
    course: "C++"
  },
  {
    name: "Aman",
    age: 18,
    course: "Java"
  }
]

Explanation

Both conditions must be true:

age >= 15
AND
age <= 18

Q6. Find Students Studying Python or JavaScript

Problem Statement

Find students whose course is either Python or JavaScript.

MongoDB Command / Query

db.students.find({
  $or: [
    { course: "Python" },
    { course: "JavaScript" }
  ]
})

Expected Output

[
  {
    name: "Rahul",
    age: 16,
    course: "Python"
  },
  {
    name: "Priya",
    age: 15,
    course: "JavaScript"
  }
]

Explanation

$or is useful when any one of multiple conditions can be satisfied.


Q7. Find Students Who Are Not 16 Years Old

Problem Statement

Find all students whose age is not equal to 16.

MongoDB Command / Query

db.students.find({
  age: {
    $not: { $eq: 16 }
  }
})

Expected Output

[
  {
    name: "Priya",
    age: 15,
    course: "JavaScript"
  },
  {
    name: "Neha",
    age: 17,
    course: "C++"
  },
  {
    name: "Aman",
    age: 18,
    course: "Java"
  }
]

Explanation

The $not operator reverses the $eq condition.

So:

NOT (age = 16)

returns students whose age is anything other than 16.


Q8. Find Students Older Than 15 Who Study Python or C++

Problem Statement

Find students who are older than 15 and are studying either Python or C++.

MongoDB Command / Query

db.students.find({
  $and: [
    { age: { $gt: 15 } },
    {
      $or: [
        { course: "Python" },
        { course: "C++" }
      ]
    }
  ]
})

Expected Output

[
  {
    name: "Rahul",
    age: 16,
    course: "Python"
  },
  {
    name: "Neha",
    age: 17,
    course: "C++"
  }
]

Explanation

This query combines $and and $or.

The student must:

  1. Be older than 15
  2. Study either Python or C++

Q9. Find Students Who Are Not Older Than 16 and Not Studying Java

Problem Statement

Find students whose age is not greater than 16 and whose course is not Java.

MongoDB Command / Query

db.students.find({
  $and: [
    { age: { $not: { $gt: 16 } } },
    { course: { $ne: "Java" } }
  ]
})

Expected Output

[
  {
    name: "Priya",
    age: 15,
    course: "JavaScript"
  },
  {
    name: "Rahul",
    age: 16,
    course: "Python"
  }
]

Explanation

Two conditions are applied:

NOT (age > 16)
AND
course != Java

Q10. Find Students Who Are Either Under 16 or Over 17

Problem Statement

Find students whose age is either less than 16 or greater than 17.

MongoDB Command / Query

db.students.find({
  $or: [
    { age: { $lt: 16 } },
    { age: { $gt: 17 } }
  ]
})

Expected Output

[
  {
    name: "Priya",
    age: 15,
    course: "JavaScript"
  },
  {
    name: "Aman",
    age: 18,
    course: "Java"
  }
]

Explanation

The $or operator checks both conditions:

age &lt; 16
OR
age > 17

Students aged exactly 16 or 17 are not included.

Key Takeaways

  • $and requires all conditions to be true.
  • $or requires at least one condition to be true.
  • $nor returns documents that fail all specified conditions.
  • $not reverses the result of another query operator.
  • Logical operators can be combined with comparison operators.
  • $and and $or can be nested inside one another.
  • $not is useful for reversing conditions such as $gt and $eq.
  • Logical operators make complex MongoDB queries easier to build.
  • They can be used with find(), update filters, and delete filters.
  • Always check your conditions carefully before using logical operators with updateMany() or deleteMany().

FAQs

1. What are logical operators in MongoDB?

Logical operators are used to combine or reverse query conditions. The main logical operators are $and, $or, $nor, and $not.

2. What is the use of $and in MongoDB?

$and returns documents only when all specified conditions are true.

db.students.find({
  $and: [
    { age: { $gt: 15 } },
    { course: "Python" }
  ]
})

3. What is the use of $or in MongoDB?

$or returns documents when at least one of the specified conditions is true.

db.students.find({
  $or: [
    { course: "Python" },
    { course: "Java" }
  ]
})

4. What is $nor in MongoDB?

$nor selects documents that fail all the conditions specified inside the $nor array.

db.students.find({
  $nor: [
    { course: "Python" },
    { course: "Java" }
  ]
})

5. What does $not do in MongoDB?

$not reverses the result of another query operator.

For example:

db.students.find({
  age: { $not: { $gt: 17 } }
})

This finds students whose age is not greater than 17.

6. Can MongoDB logical operators be combined?

Yes. Logical operators can be nested and combined with comparison operators.

db.students.find({
  $and: [
    { age: { $gt: 15 } },
    {
      $or: [
        { course: "Python" },
        { course: "Java" }
      ]
    }
  ]
})

7. What is the difference between $and and $or in MongoDB?

$and requires every condition to be true, while $or requires at least one condition to be true.

$and → Condition 1 AND Condition 2
$or  → Condition 1 OR Condition 2

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top