Open In App

MongoDB – Comparison Query Operators

Last Updated : 04 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

MongoDB provides powerful comparison query operators to filter and retrieve documents based on field values. These operators help developers perform precise queries, enabling efficient data retrieval and manipulation. MongoDB uses various comparison query operators to compare the values of the documents

In this article, we will explore MongoDB comparison query operators, their syntax, use cases, and practical examples to help us master data filtering in MongoDB. Whether we’re a beginner or an experienced developer, this article will ensure we understand how to use these operators effectively.

What are Comparison Query Operators in MongoDB?

Comparison query operators allow users to filter documents based on specific conditions. These operators can be used to find records that match, differ, or fall within a range of values. They include operators like $eq, $ne, $gt, $lt, $gte, $lte, $in, and $nin. These operators help in efficiently querying and retrieving documents based on numerical, string, or date field comparisons.

  • To filter documents based on specific field values.
  • To compare numerical, string, or date fields.
  • To refine search queries for optimized 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.

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

Best Practices for Using MongoDB Comparison Operators

To optimize performance and ensure efficiency, follow these best practices when using comparison operators:

  1. Use Indexing: Ensure fields used in comparisons are indexed to speed up query execution.
  2. Avoid Large Scans: Use selective queries to prevent full collection scans.
  3. Optimize $in Queries: Avoid large arrays in $in queries as they can slow down performance.
  4. Use Projection: Limit returned fields using projections to reduce query load.
  5. Utilize $expr: For comparing two fields within the same document, use $expr for efficient comparisons.

Conclusion

In this article, we learned about different comparison operators in MongoDB. We discussed the definition and use of MongoDB comparison operators. There are many operators that are used for comparison query in MongoDB. We have covered various comparison operators and understood their workings with examples. By implementing these operators effectively, we can enhance our MongoDB query performance and ensure efficient data retrieval for your applications.



Next Article

Similar Reads