Comparison Operator in MongoDB

By | August 13, 2022

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

$eq

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"})

$ne

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.

$gt

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.

$gte

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.

$lt

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.

$lte

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.

$in

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.

$nin

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.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments