Lesson 14 of 50 – Comparison Operators in MongoDB
28%

Comparison Operators in MongoDB

MongoDB comparison operators allow us to compare document field values with specific values. They are useful when searching for students by age, marks, course fees, employee salaries, product prices, and other information.

In this lesson, you will learn how to use $eq, $ne, $gt, $gte, $lt, $lte, $in, and $nin.

1. What Are Comparison Operators?

Comparison operators are used inside MongoDB query filters to compare a field with a given value.

Example:

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

This query finds students whose age is greater than 20.

Comparison operators are especially useful when you need to retrieve documents that satisfy numerical or value-based conditions.

2. Prepare the Sample Database

Select the database:

use school

Insert sample student documents:

db.students.insertMany([
    {
        name: "Rahul",
        age: 18,
        marks: 55,
        course: "ADCA",
        fees: 5000
    },
    {
        name: "Amit",
        age: 20,
        marks: 75,
        course: "Python",
        fees: 8000
    },
    {
        name: "Priya",
        age: 22,
        marks: 90,
        course: "Web Development",
        fees: 10000
    },
    {
        name: "Neha",
        age: 21,
        marks: 65,
        course: "Python",
        fees: 8000
    },
    {
        name: "Ravi",
        age: 24,
        marks: 40,
        course: "ADCA",
        fees: 5000
    }
])

Note: Run the insertion example only once to avoid duplicate sample documents.

3. Equal To Operator ($eq)

The $eq operator matches documents where a field equals the specified value.

Example 1: Find Students Aged 20
db.students.find({
    age: { $eq: 20 }
})
Example 2: Find Students Enrolled in Python
db.students.find({
    course: { $eq: "Python" }
})
Example 3: Find Students with 90 Marks
db.students.find({
    marks: { $eq: 90 }
})

You can also use direct equality without writing $eq.

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

Both forms match documents whose age is equal to 20.

4. Not Equal To Operator ($ne)

The $ne operator matches documents where a field value is not equal to the specified value. It can also match documents where the field is missing.

Example 1: Students Not Aged 20
db.students.find({
    age: { $ne: 20 }
})
Example 2: Students Not Enrolled in ADCA
db.students.find({
    course: { $ne: "ADCA" }
})
Example 3: Students Whose Marks Are Not 90
db.students.find({
    marks: { $ne: 90 }
})

Use $ne when you want to exclude a particular value from your search results.

5. Greater Than Operator ($gt)

The $gt operator matches documents where a field value is greater than the specified value.

Example 1: Students Older Than 20
db.students.find({
    age: { $gt: 20 }
})
Example 2: Students Scoring More Than 70
db.students.find({
    marks: { $gt: 70 }
})
Example 3: Courses with Fees Above 5,000
db.students.find({
    fees: { $gt: 5000 }
})

The specified value itself is not included in a greater-than comparison.

6. Greater Than or Equal To ($gte)

The $gte operator matches documents where a field value is greater than or equal to the specified value.

Example 1: Students Aged 21 or Older
db.students.find({
    age: { $gte: 21 }
})
Example 2: Students Scoring at Least 75
db.students.find({
    marks: { $gte: 75 }
})
Example 3: Courses with Fees of 8,000 or More
db.students.find({
    fees: { $gte: 8000 }
})

Unlike $gt, $gte includes documents whose field value equals the specified value.

7. Less Than Operator ($lt)

The $lt operator matches documents where a field value is less than the specified value.

Example 1: Students Younger Than 21
db.students.find({
    age: { $lt: 21 }
})
Example 2: Students Scoring Below 60
db.students.find({
    marks: { $lt: 60 }
})
Example 3: Courses with Fees Below 8,000
db.students.find({
    fees: { $lt: 8000 }
})

The specified value itself is not included in a less-than comparison.

8. Less Than or Equal To ($lte)

The $lte operator matches documents where a field value is less than or equal to the specified value.

Example 1: Students Aged 20 or Younger
db.students.find({
    age: { $lte: 20 }
})
Example 2: Students Scoring 65 or Less
db.students.find({
    marks: { $lte: 65 }
})
Example 3: Courses with Fees of 5,000 or Less
db.students.find({
    fees: { $lte: 5000 }
})

The $lte operator includes documents whose field value equals the specified value.

9. In Operator ($in)

The $in operator matches a field against any value in a specified array.

Example 1: Find ADCA or Python Students
db.students.find({
    course: {
        $in: ["ADCA", "Python"]
    }
})
Example 2: Find Students Aged 20 or 22
db.students.find({
    age: {
        $in: [20, 22]
    }
})
Example 3: Find Students Scoring 40 or 90
db.students.find({
    marks: {
        $in: [40, 90]
    }
})

This operator is useful when you want to match one field against several possible values.

10. Not In Operator ($nin)

The $nin operator matches documents whose field value is not in the specified array. It can also match documents where the field is missing.

Example 1: Exclude ADCA and Python Students
db.students.find({
    course: {
        $nin: ["ADCA", "Python"]
    }
})
Example 2: Exclude Ages 20 and 22
db.students.find({
    age: {
        $nin: [20, 22]
    }
})
Example 3: Exclude Marks of 40 and 55
db.students.find({
    marks: {
        $nin: [40, 55]
    }
})

Use $nin when you need to exclude multiple values from the search results.

11. Combine Comparison Operators

You can combine multiple conditions in a single query to retrieve more specific results.

Example 1: Students Aged Between 20 and 22
db.students.find({
    age: {
        $gte: 20,
        $lte: 22
    }
})

This includes students aged 20, 21, or 22.

Example 2: Students Scoring Between 60 and 90
db.students.find({
    marks: {
        $gte: 60,
        $lte: 90
    }
})
Example 3: Python Students Scoring Above 70
db.students.find({
    course: "Python",
    marks: { $gt: 70 }
})
Example 4: Students with Fees Between 5,000 and 8,000
db.students.find({
    fees: {
        $gte: 5000,
        $lte: 8000
    }
})

When multiple conditions are specified for the same field, MongoDB applies them together.

12. Comparison Operators Quick Reference

Operator Meaning Example
$eq Equal to { age: { $eq: 20 } }
$ne Not equal to { age: { $ne: 20 } }
$gt Greater than { age: { $gt: 20 } }
$gte Greater than or equal to { age: { $gte: 20 } }
$lt Less than { age: { $lt: 20 } }
$lte Less than or equal to { age: { $lte: 20 } }
$in Matches any listed value { age: { $in: [20, 22] } }
$nin Excludes listed values { age: { $nin: [20, 22] } }

📝 Practical Assignment

Use the school database and the students collection to complete these tasks.

  1. Find students whose age is equal to 20 using $eq.
  2. Find students whose age is not equal to 20 using $ne.
  3. Find students whose marks are greater than 70.
  4. Find students whose marks are greater than or equal to 75.
  5. Find students whose age is less than 22.
  6. Find students whose marks are less than or equal to 65.
  7. Find students enrolled in ADCA or Python using $in.
  8. Find students whose course is neither ADCA nor Python using $nin.
  9. Find students aged between 20 and 22, inclusive.
  10. Find students whose fees are greater than 5,000 and marks are at least 70.

🧠 Quick Quiz

1. Which operator means greater than?


2. Which operator matches any value in an array?


3. Which operator means less than or equal to?