Open In App

MongoDB – Comparison Query Operators

Last Updated : 23 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Comparison Query Operators in MongoDB are used to filter documents based on some specific criteria within their fields.

MongoDB uses various comparison query operators to compare the values of the documents. The following table contains the 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

Let’s look at the examples of comparison operators. We will cover each comparison operator in MongoDB.

In the following examples, we are working with:

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

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

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

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

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

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

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

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

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

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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads