MongoDB

How to use $eq operator in MongoDB

MongoDB is a database management system to store and retrieve a bulk amount of data in an effective manner. Like other databases, MongoDB also has several data manipulation commands and operators that can be used to store as well as retrieve data from a collection of documents. Several commonly used operators in MongoDB include $eq, $lt, $and, $or etc. These commands and operators relate to other databases as well; for instance, the $eq operators perform the same operation as where clause in SQL databases.

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:

{“field”:{$eq:“value”}}

Moreover, the extended syntax when used in “find()” method is written below:

db.collection-name.find({“field”:{$eq:“value”}})

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:

> use linuxhint

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”:

> db.createCollection("distros")

After this, we have added several documents; each document refers to a distribution of Linux:

> db.distros.insert([
                       {
                        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:

> db.distros.find().pretty()

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”:

> db.distros.find({description:{$eq:"Debian distro"}}).pretty()

Or the above query can also be replaced by the one mentioned below; it performs the same action as of above:

> db.distros.find({description: "Debian distro"}).pretty()

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:

> db.distros.find({cat: {$eq: "C"}}).pretty()

Moreover, the following MongoDB query will display the document that have values “A” and “B” in “cat” field:

> db.distros.find({cat: {$eq: ["A", "B"]}}).pretty()

Or you can execute the above command by following way as well; in both cases, the output will be the same:

> db.distros.find({cat: [“A”, “B”]}).pretty()

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”:

> db.distros.find({num: {$eq: 10}}).pretty()

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.

About the author

Adnan Shabbir