In this article, a Comparison Query Operator operator $eq will be explained in the context of MongoDB:
Why $eq operator is used in MongoDB
The $eq operator in MongoDB can be used to retrieve the document(s) from a collection. It is helpful when you have a collection that contains hundreds of documents and you want to retrieve only those documents that match your required field and value. This operation enables you to fetch the required document quickly and smoothly.
The syntax to use $eq is written below:
Moreover, the extended syntax when used in “find()” method is written below:
Where:
- “db” refers to the database on which the operation will be performed:
- “collection-name” indicates the name of collection from which the data will be retrieved:
- “find()” is a MongoDB method to retrieve documents from collections:
- “field” and “value” refer to the name of field and its assigned value in a document:
How to use $eq operator in MongoDB
To use $eq operator, you must have a MongoDB database that can be created using the following mongo shell command:
Step 1: Create MongoDB database
Here, “use” keyword will create and connect you to “linuxhint” database:
Step 2: Create collection and add documents
Once database is created; use the following command to create a collection inside a database:
For instance, we have created a collection named as “distros”:
After this, we have added several documents; each document refers to a distribution of Linux:
{
title: "Debian",
description: "Linux distro",
num: 20,
cat: ["A", "B"]
},
{
title: "Ubuntu",
description: "Debian distro",
num: 15,
cat: ["B", "D"]
},
{
title: "AntiX",
description: "Debian distro",
num: 10,
cat: ["C", "D"]
}
])
After insertion, the below stated command will show the documents inside “distros” collection:
Note: The above steps are optional; if you have already created and inserted documents in the database; then you can directory to the next section:
Example 1: Match a string value in a field
The “distros” collection of “linuxhint” database contains three documents; for instance, the below mentioned command can be used to get those documents whose “description” value is “Debian distro”:
Or the above query can also be replaced by the one mentioned below; it performs the same action as of above:
Example 2: Match an array value in a field
There are two ways to use the $eq operator to match an array value in a field: This example refers to matching a single array value from an array field. In our case, the command mentioned below will display those documents in which the “cat” field matches only the “C” value:
Moreover, the following MongoDB query will display the document that have values “A” and “B” in “cat” field:
Or you can execute the above command by following way as well; in both cases, the output will be the same:
Example 3: Match a numeric value in a field
With the help of $eq operator you can match numeric values as well; for instance, the documents in “distros” collection contains a field “num” that contains numeric values: so, the command below will display the document that matches “num=10”:
Note The pretty() method in the examples is used to get a clear output of the commands.
Conclusion
MongoDB supports a long list of operators that belong to the comparison, logical, and elements category. The operators of the “Comparison Query Class” are practiced to provide the output after comparison. In this article, the use of the $eq operator is described to retrieve the documents in MongoDB. It is used to display only those documents that match the value of a specific field. The datatypes of the fields that can be retrieved using the $eq operator include strings, arrays, and numeric.