MongoDB - Comparison Query Operators
Last Updated :
18 Sep, 2025
In MongoDB, comparison query operators enable users to filter documents according to specific conditions. They allow you to match, exclude, or select records within a range of values, making it easier to query and retrieve documents based on numerical, string, or date field comparisons. These operators help refine search queries and improve performance.
Below is a list of commonly used MongoDB comparison query operators:
Operators | Description |
---|
$eq | Matches the values of the fields that are equal to a specified value. |
$ne | Matches all values of the field that are not equal to a specified value. |
$gt | Matches values of the fields that are greater than a specified value. |
$gte | Matches values of the fields that are greater than equal to the specified value. |
$lt | Matches values of the fields that are less than a specified value |
$lte | Matches values of the fields that are less than equal to the specified value |
$in | Matches any of the values specified in an array. |
$nin | Matches none of the values specified in an array. |
MongoDB Comparison Operator Examples
For better understanding, let’s look at real-world examples using a sample "contributor" collection in a database named "GeeksforGeeks".
- Database: GeeksforGeeks
- Collection: contributor
- Document: three documents that contain the details of the contributors in the form of field-value pairs.

Example 1: Using $nin
operator:
In this example, we are retrieving only those employee’s documents whose name is not "Amit" or "Suman".
Query:
db.contributor.find({name: {$nin: ["Amit", "Suman"]}}).pretty()
Output

Example 2: Using $in
operator
In this example, we are retrieving only those employee's documents whose name is either "Amit" or "Suman".
Query:
db.contributor.find({name: {$in: ["Amit", "Suman"]}}).pretty()
Output:

Example 3: Using $lt
operator
In this example, we are selecting those documents where the value of the salary field is less than 2000.
Query:
db.contributor.find({salary: {$lt: 2000}}).pretty()
Output:

Example 4: Using $eq
operator
In this example, we are selecting those documents where the value of the branch field is equal to "CSE".
Query:
db.contributor.find({branch: {$eq: "CSE"}}).pretty()
Output:

Example 5: Using $ne
operator
In this example, we are selecting those documents where the value of the branch field is not equal to CSE.
Query:
db.contributor.find({branch: {$ne: "CSE"}}).pretty()
Output:

Example 6: Matching values using $gt
operator:
In this example, we are selecting those documents where the value of the salary field is greater than 1000.
Query:
db.contributor.find({salary: {$gt: 1000}}).pretty()
Output:

Example 7: Using $gte
operator
In this example, we are selecting those documents where the value of the joiningYear field is greater than equals to 2017.
Query:
db.contributor.find({joiningYear: {$gte: 2017}})
Output:

Example 8: Using $lte
operator
In this example, we are selecting those documents where the value of the salary field is less than equals to 1000.
Query:
db.contributor.find({salary: {$lte: 1000}}).pretty()
Output:
Explore
MongoDB Tutorial
7 min read
Introduction
Installation
Basics of MongoDB
MongoDB Methods
Comparison Operators
Logical Operators
Arithmetic Operators
Field Update Operators
Array Expression Operators