MongoDB – Equality Operator $eq

MongoDB provides different types of comparison operators and an equality operator($eq) is one of them. The equality operator( $eq ) is used to match the documents where the value of the field is equal to the specified value. In other words, the $eq operator is used to specify the equality condition.

Important Points:

  • If the given value is a document, then the order of the fields in the document is important.
  • If the given value is an array, then MongoDB matches the documents where the field contains an element that exactly matches the specified array.

Syntax:

{field: {$eq: value}}
or
{field: value}

In the following examples, we are working with:

Database: GeeksforGeeks
Collection: employee
Document: five documents that contain the details of the employees in the form of field-value pairs.




 
Example #1:
In this example, we are selecting those documents where the value of the salary field is equal to 30000.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.employee.find({salary: {$eq: 30000}}).pretty()

chevron_right


It is equivalent to –

filter_none

edit
close

play_arrow

link
brightness_4
code

db.employee.find({salary: 30000}).pretty()

chevron_right


 
Example #2:
In this example, we are selecting those documents where the first name of the employee is equal to Amu. or in other words, in this example, we are specifying conditions on the field in the embedded document using dot notation.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.employee.find({"name.first": {$eq: "Amu"}}).pretty()

chevron_right


It is equivalent to:

filter_none

edit
close

play_arrow

link
brightness_4
code

db.employee.find({"name.first": "Amu"}).pretty()

chevron_right


 
Example #3:
In this example, we are selecting those documents where the language array contains an element with value “C++”.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.employee.find({language: {$eq: "C++"}}).pretty()

chevron_right


It is equivalent to:

filter_none

edit
close

play_arrow

link
brightness_4
code

db.employee.find({language: "C++"}).pretty()

chevron_right


 
Example #4:
In this example, we are selecting those documents where the language array is equal to the specified array.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.employee.find({language: {$eq: ["C#", "Java"]}}).pretty()

chevron_right


It is equivalent to:

filter_none

edit
close

play_arrow

link
brightness_4
code

db.employee.find({language:["C#", "Java"]}).pretty()

chevron_right



Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.




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.