Comparison Operator in MongoDB

In this post, we will learn some comparison operator in MongoDB. The comparison operator in MongoDB can be used to return the data based on the value comparison in the fields. The equivalent of Java can be read in this post.

The syntax of using the comparison operator in MongoDb is:

{<field>: {<operator>: <value>}}

The comparison operator in MongoDB are:

NameDescription
$eqThis matches all the values in the field which is equal to the given value.
$neThis matches all the values in the field which is not equal to the given value.
$gtThis matches all the values in the field which is greater than the given value.
$gteThis matches all the values in the field which is greater than or equal to the given value.
$inThis matches any of the values in the field specified in the given array of elements.
$ninThis matches none of the values in the field specified in the given array of elements.
$ltThis matches all the values in the field which is less than the given value.
$lteThis matches all the values in the field which is less than or equal to the given value.
Table: Comparison operator in MongoDB

MongoDB comparison operator with an example

MongoDB $eq Operator Example

The $eq operator in MongoDB matches all the values in the field which is equal to the given value.

Example:

db.employee.find({"name":{"$eq":"Coder Sathi"}})

The above query returns the documents that match the name which has the value Coder Sathi.

In MongoDB, the default operator is $eq and this is equivalent to the following query:

db.employee.find({"name":"Coder Sathi"})

MongoDB $ne Operator Example

The $ne operator in MongoDB matches all the values in the field which is not equal to the given value.

Example:

db.employee.find({"name":{"$ne":"Coder Sathi"}})

The above query returns all the documents that do not match the name Coder Sathi.

It means, that when we have to exclude some documents based on the given value, then we can use $ne operator.

MongoDB $gt Operator Example

The $gt operator in MongoDB matches all the documents with the field having a greater value than the given value.

Example:

db.employee.find({"salary":{"$gt":50000}})

The above query returns the documents which have a salary greater than 50000.

MongoDB $gte Operator Example

The $gte operator in MongoDB matches all the documents with the field having a value greater than or equal to the given value.

Example:

db.employee.find({"salary":{"$gte":50000}})

The above query returns the documents which have a salary greater than or equal to 50000.

MongoDB $lt Operator Example

The $lt operator in MongoDB matches all the documents with the field having a value less than the given value.

Example:

db.employee.find({"salary":{"$lt":50000}})

The above query returns the documents which have a salary of less than 50000.

MongoDB $lte Operator Example

The $lte operator in MongoDB matches all the documents with the field which has a value less than or equal to the given value.

Example:

db.employee.find({"salary":{"$lte":50000}})

The above query returns the documents which have a salary less than or equal to 50000.

MongoDB $in Operator Example

The $in operator in MongoDB matches any values given in the array of elements.

Example:

db.employee.find({"department":{"$in":["Engineering", "HR"]}})

The above query returns the documents from employee collection which match any of the departments either Engineering or HR.

MongoDB $nin Operator Example

The $nin operator in MongoDB matches any values given in the array of elements.

Example:

db.employee.find({"department":{"$nin":["Engineering", "HR"]}})

The above query returns the documents from employee collection which match neither Engineering nor HR.

Summary

In this post, we learned the various types of comparison operators in MongoDB with an example.

Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments