Paramclasses Academy - Blog

Tutorials Details

←
Post 13 of 35
→

MongoDB Query Operators: Comparison and Logical Operators Explained

MongoDB Query Operators: Comparison and Logical Operators Explained

November 5, 2026

 

MongoDB Query Operators: Comparison and Logical Operators Explained

Welcome to Param Classes!

MongoDB provides powerful query operators that help developers search, filter, and retrieve documents from a collection based on specific conditions.

When working with databases, you may need to find students who scored more than 80 marks, employees whose salary is below a certain amount, or customers who belong to a particular city.

MongoDB query operators make these operations simple and flexible.

In this tutorial by Param Classes, you will learn about two important types of MongoDB query operators:

  1. Comparison Operators

  2. Logical Operators

We will understand each operator with practical examples using a student collection.

✦ 1. What Are MongoDB Query Operators?

MongoDB query operators are special keywords that allow you to specify conditions when searching for documents.

They are commonly used with methods such as:

  • find()

  • findOne()

  • updateOne()

  • updateMany()

  • deleteOne()

  • deleteMany()

Example:

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

Explanation:

  • db.students refers to the students collection.

  • find() retrieves matching documents.

  • age is the field being checked.

  • $gt is a comparison operator.

  • 20 is the comparison value.

This query retrieves students whose age is greater than 20.

✦ 2. Types of MongoDB Query Operators

MongoDB provides several categories of query operators.

Some important categories are:

Operator Category Purpose
Comparison Operators Compare field values
Logical Operators Combine multiple conditions
Element Operators Check field existence and data type
Evaluation Operators Match values using expressions or regular expressions
Array Operators Search within array fields

In this tutorial, we will focus mainly on Comparison and Logical Operators.

✦ 3. Create a Collection with Sample Data

Before practising query operators, let's create a collection named students.

Run the following command in MongoDB Shell:

db.students.insertMany([
    {
        name: "Rahul",
        age: 22,
        course: "MongoDB",
        marks: 85,
        city: "Raipur",
        status: "Active"
    },
    {
        name: "Amit",
        age: 24,
        course: "Java",
        marks: 78,
        city: "Delhi",
        status: "Active"
    },
    {
        name: "Priya",
        age: 21,
        course: "Python",
        marks: 92,
        city: "Mumbai",
        status: "Active"
    },
    {
        name: "Neha",
        age: 23,
        course: "MongoDB",
        marks: 65,
        city: "Raipur",
        status: "Inactive"
    },
    {
        name: "Rohit",
        age: 20,
        course: "Java",
        marks: 70,
        city: "Bhopal",
        status: "Active"
    }
])

Now we have five student documents to practise MongoDB query operators.

✦ 4. MongoDB Comparison Operators

Comparison operators are used to compare field values with specified values.

They help retrieve documents based on conditions such as greater than, less than, equal to, or not equal to.

Important comparison operators include:

Operator Description
$eq Equal to
$ne Not equal to
$gt Greater than
$gte Greater than or equal to
$lt Less than
$lte Less than or equal to
$in Matches any value in an array
$nin Does not match any value in an array

Let's understand them one by one.

4.1 $eq – Equal To

The $eq operator matches documents where a field value is equal to the specified value.

Example:

Find students whose course is MongoDB.

db.students.find({
    course: { $eq: "MongoDB" }
})

Explanation:

This query retrieves students whose course field is exactly equal to "MongoDB".

You can also write the same query without $eq:

db.students.find({
    course: "MongoDB"
})

Both queries perform an equality match.

4.2 $ne – Not Equal To

The $ne operator matches documents where a field value is not equal to the specified value.

Example:

Find students whose course is not Java.

db.students.find({
    course: { $ne: "Java" }
})

Explanation:

This query retrieves students whose course is not equal to Java.

For fields that do not exist in a document, $ne can also match that document. Keep this behavior in mind when working with incomplete data.

4.3 $gt – Greater Than

The $gt operator matches values greater than the specified value.

Example:

Find students whose age is greater than 21.

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

Expected Matching Students:

  • Rahul – Age 22

  • Amit – Age 24

  • Neha – Age 23

4.4 $gte – Greater Than or Equal To

The $gte operator matches values greater than or equal to the specified value.

Example:

Find students whose marks are greater than or equal to 85.

db.students.find({
    marks: { $gte: 85 }
})

Expected Matching Students:

  • Rahul – 85 marks

  • Priya – 92 marks

4.5 $lt – Less Than

The $lt operator matches values less than the specified value.

Example:

Find students whose age is less than 22.

db.students.find({
    age: { $lt: 22 }
})

Expected Matching Students:

  • Priya – Age 21

  • Rohit – Age 20

4.6 $lte – Less Than or Equal To

The $lte operator matches values less than or equal to the specified value.

Example:

Find students whose marks are less than or equal to 78.

db.students.find({
    marks: { $lte: 78 }
})

Expected Matching Students:

  • Amit – 78 marks

  • Neha – 65 marks

  • Rohit – 70 marks

4.7 $in – Match Any Value

The $in operator matches documents where a field value equals any value from the specified array.

Example:

Find students enrolled in MongoDB or Python.

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

Explanation:

MongoDB checks whether the course field matches either MongoDB or Python.

4.8 $nin – Not Match Any Value

The $nin operator matches documents where a field value does not match any value in the specified array.

Example:

Find students whose course is neither MongoDB nor Python.

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

Explanation:

This query retrieves documents where the course field is not MongoDB or Python. It can also match documents where the field does not exist.

✦ 5. MongoDB Logical Operators

Logical operators are used to combine multiple conditions in a query.

They help retrieve documents when you need to apply more than one condition.

Important logical operators include:

Operator Description
$and Matches documents when all conditions are true
$or Matches documents when at least one condition is true
$not Inverts the effect of a query expression
$nor Matches documents when all specified conditions are false

Let's understand each operator with examples.

5.1 $and – AND Operator

The $and operator returns documents that match all specified conditions.

Example:

Find students whose age is greater than 21 and marks are greater than 80.

db.students.find({
    $and: [
        { age: { $gt: 21 } },
        { marks: { $gt: 80 } }
    ]
})

Explanation:

Both conditions must be true:

  • Age must be greater than 21.

  • Marks must be greater than 80.

Only Rahul matches both conditions in our sample data.

Alternative Syntax:

MongoDB also supports implicit AND when multiple different fields are specified in one filter.

db.students.find({
    age: { $gt: 21 },
    marks: { $gt: 80 }
})

5.2 $or – OR Operator

The $or operator returns documents that match at least one of the specified conditions.

Example:

Find students who belong to Raipur or are enrolled in Java.

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

Explanation:

A student will be returned if either condition is true:

  • City is Raipur.

  • Course is Java.

Both conditions do not need to be true.

5.3 $not – NOT Operator

The $not operator inverts the effect of a query expression for a field.

Example:

Find students whose marks are not greater than 80.

db.students.find({
    marks: {
        $not: { $gt: 80 }
    }
})

Explanation:

This query matches documents where marks do not satisfy the greater-than-80 condition. It can also match documents where the marks field is missing.

For a simple not-equal comparison, use $ne:

db.students.find({
    course: { $ne: "Java" }
})

5.4 $nor – NOR Operator

The $nor operator returns documents that fail all the specified conditions.

Example:

Find students who are neither from Raipur nor from Delhi.

db.students.find({
    $nor: [
        { city: "Raipur" },
        { city: "Delhi" }
    ]
})

Explanation:

MongoDB excludes documents matching either condition and returns documents that match neither condition.

✦ 6. Difference Between Comparison and Logical Operators

Comparison and logical operators are both used for filtering documents, but they have different purposes.

Comparison Operators Logical Operators
Compare field values. Combine or invert query conditions.
Work with values such as numbers and strings. Work with one or more query expressions.
Examples: $gt, $lt, $eq, $ne. Examples: $and, $or, $not, $nor.
Useful for value-based searches. Useful for condition-based searches.

Comparison Operator Example:

db.students.find({
    marks: { $gt: 80 }
})

Logical Operator Example:

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

✦ 7. Combining Comparison and Logical Operators

MongoDB allows you to combine comparison and logical operators in a single query.

This is useful when you need to apply multiple conditions to retrieve specific documents.

Example 1: Students Older Than 21 AND From Raipur

db.students.find({
    $and: [
        { age: { $gt: 21 } },
        { city: "Raipur" }
    ]
})

Example 2: Students With Marks Above 80 OR Age Below 21

db.students.find({
    $or: [
        { marks: { $gt: 80 } },
        { age: { $lt: 21 } }
    ]
})

Example 3: Students Enrolled in MongoDB OR Python With Marks Above 80

db.students.find({
    $and: [
        {
            course: {
                $in: ["MongoDB", "Python"]
            }
        },
        {
            marks: { $gt: 80 }
        }
    ]
})

Example 4: Students Who Are Not From Delhi AND Have Marks Above 70

db.students.find({
    city: { $ne: "Delhi" },
    marks: { $gt: 70 }
})

These examples show how MongoDB operators can be combined to create more specific queries.

✦ 8. Practical Examples of MongoDB Query Operators

Let's practise some real-world search conditions.

Find students with marks between 70 and 90:

db.students.find({
    marks: {
        $gte: 70,
        $lte: 90
    }
})

Find students from Raipur or Mumbai:

db.students.find({
    city: {
        $in: ["Raipur", "Mumbai"]
    }
})

Find students who are Active and have marks above 80:

db.students.find({
    status: "Active",
    marks: { $gt: 80 }
})

Find students who are not enrolled in Java or Python:

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

Find students whose age is at least 21 and whose marks are at least 85:

db.students.find({
    age: { $gte: 21 },
    marks: { $gte: 85 }
})

✦ 9. Common Mistakes to Avoid

Beginners may face some common problems while using MongoDB query operators.

  1. Incorrect operator spelling: Always use the correct operator name, such as $gte instead of $get.

  2. Missing dollar sign: MongoDB operators begin with $.

  3. Incorrect data types: A numeric value and a string containing digits are not necessarily treated the same way.

  4. Incorrect logical conditions: Remember that $and requires all conditions to match, while $or requires at least one.

  5. Confusing $gt and $gte: $gt excludes the specified value, whereas $gte includes it.

  6. Ignoring missing fields: Operators such as $ne and $nin may also match documents where the field is missing.

  7. Incorrect field names: MongoDB field names are case-sensitive.

✦ 10. Practical Exercise

Now it is time to practise MongoDB query operators.

Use the students collection created in this tutorial and complete these tasks:

  1. Find students whose age is greater than 22.

  2. Find students whose marks are equal to 85.

  3. Find students whose marks are less than or equal to 78.

  4. Find students who are not enrolled in Java.

  5. Find students enrolled in MongoDB or Python.

  6. Find students who belong to Raipur or Delhi.

  7. Find students whose age is greater than 20 AND marks are greater than 75.

  8. Find students whose course is Java OR marks are greater than 90.

  9. Find students whose city is neither Raipur nor Mumbai.

  10. Find Active students whose marks are greater than or equal to 80.

Try writing these queries yourself in MongoDB Shell.

✦ 11. Conclusion

In this tutorial, we learned about MongoDB Comparison and Logical Operators and how they help us retrieve documents based on specific conditions.

We covered:

  • Comparison operators: $eq, $ne, $gt, $gte, $lt, $lte, $in, and $nin.

  • Logical operators: $and, $or, $not, and $nor.

  • Combining multiple operators in a single query.

  • Practical examples using a student collection.

These operators are essential for building database-driven applications because they allow developers to search and filter data according to application requirements.

Keep practising these commands to improve your MongoDB skills.

Learn Today, Grow Tomorrow — Param Classes.

←
Post 13 of 35
→