MongoDB – Comparison Query Operators

MongoDB uses various comparison query operators to compare the values of the documents. The following table contains the comparison query operators:

Operators Description
$eq It is used to match the values of the fields that are equal to a specified value.
$ne It is used to match all values of the field that are not equal to a specified value.
$gt It is used to match values of the fields that are greater than a specified value.
$gte It is used to match values of the fields that are greater than equal to the specified value.
$lt It is used to match values of the fields that are less than a specified valueo
$lte It is used to match values of the fields that are less than equals to the specified value
$in It is used to match any of the values specified in an array.
$nin It is used to match none of the values specified in an array.

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.

Matching values using $nin operator:

In this example, we are retrieving only those employee’s documents whose name is not Amit or Suman.



filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({name: {$nin: ["Amit", "Suman"]}}).pretty()

chevron_right


Matching values using $in operator:

In this example, we are retrieving only those employee’s documents whose name is either Amit or Suman.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({name: {$in: ["Amit", "Suman"]}}).pretty()

chevron_right


Matching values using $lt operator:

In this example, we are selecting those documents where the value of the salary field is less than 2000.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({salary: {$lt: 2000}}).pretty()

chevron_right


Matching values using $eq operator:

In this example, we are selecting those documents where the value of the branch field is equal to CSE.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({branch: {$eq: "CSE"}}).pretty()

chevron_right


Matching values using $ne operator:

In this example, we are selecting those documents where the value of the branch field is not equal to CSE.



filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({branch: {$ne: "CSE"}}).pretty()

chevron_right


Matching values using $gt operator:

In this example, we are selecting those documents where the value of the salary field is greater than 1000.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({salary: {$gt: 1000}}).pretty()

chevron_right


Matching values using $gte operator:

In this example, we are selecting those documents where the value of the joiningYear field is greater than equals to 2017.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({joiningYear: {$gte: 2017}})

chevron_right


Matching values using $lte operator:

In this example, we are selecting those documents where the value of the salary field is less than equals to 1000.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.find({salary: {$lte: 1000}}).pretty()

chevron_right





My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.