Open In App

MongoDB - Comparison Query Operators

Last Updated : 18 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

OperatorsDescription
$eqMatches the values of the fields that are equal to a specified value.
$neMatches all values of the field that are not equal to a specified value.
$gtMatches values of the fields that are greater than a specified value.
$gteMatches values of the fields that are greater than equal to the specified value.
$ltMatches values of the fields that are less than a specified value
$lteMatches values of the fields that are less than equal to the specified value
$inMatches any of the values specified in an array.
$ninMatches 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.
demo database and collection

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

using $nin operator example 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:

using $in operator example 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:

using $lt operator example 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:

using $eq operator example 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:

using $ne operator example 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:

using $gt operator example 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:

using $gte operator example 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:

using $lte operator example output


Explore