MongoDB

How to use $exists operator in MongoDB

MongoDB being a NoSQL database supports a variety of operator classes to retrieve data from the database. Element query class in one of them; it consists of two operators; $exists and $type. The $exists operator comes into action when you want to get the documents that contain or do not contain any field (specified in the $exists query). The foundation of the $exists command is based on Boolean values that determine the output according to the requirement of the user.

The use of $exists operator is not limited to only getting or ignoring the complete document based on a single field. The output can be refined more by using $exists with several comparison operators like $gt, $lt, $eq, $nin.

This article aims to provide a deep insight into $exists operator of MongoDB:

How $exists operator works

The primary function of the $exists operator in MongoDB is to look for the existence of any field in a document. The $exists operator works on the basis of Boolean values i.e., true or false. The syntax of the operator is given below:

{field: {$exists: "Boolean-Value"}}

If the value is passed “true” then all the documents with the specified field are displayed. However, if the Boolean value is set to “false” then the documents other than the specified field are printed.

How to use $exists operator in MongoDB

Before getting started; it is required to connect to the MongoDB database and get the content of that database upon which the $exists operator will be applied.

The database used in this article is named as linuxhint

And the collection associated with this database: staff

Connect to your database using ubuntu terminal using the below-mentioned command:

$ sudo mongo linuxhint

Text Description automatically generated

The following content will be used in this post as an example to practice the use of $exists operator:

> db.staff.find().pretty()

Text Description automatically generated

Example 1: Basic use of $exists operator

The $exists functionality depends on the Boolean values passed to it: If you want to get the document that contains the specified field then you must pass the “true” value to it. However, when you pass a “false” value to $exists, then you will get the documents that do not contain the specified field.

This example demonstrates the use of “true” in $exists operator: The query mentioned below will retrieve all those documents that contains “experience” field:

> db.staff.find({experience: {$exists: true}}).pretty()

Text Description automatically generated

Moreover, the command mentioned below shows the usage of “false” value and the output will contain only those documents that do not have “experience” field in it:

> db.staff.find({experience: {$exists: false}}).pretty()

Text Description automatically generated

Example 2: Use of $exists with comparison operators

This example demonstrates the usage of $exists command with comparison operators. In this case, the result is displayed after double filters. The first filter applies when $exists is executed and the second comes into action when any comparison operator is called:

Using $exists with $gt operator : This comparison operator is used to display the values that satisfy the “greater-than” condition. In our “staff” collection of “linuxhint” database; there is a field named “Salary“. For instance, the query given below will give the output on following conditions:

  • Firstly, the $exists operator filters the documents that contain the “Salary” field:
  • After that, $gt operator will print only those documents that have a “Salary” value greater than “150“:
> db.staff.find({Salary: {$exists: true, $gt: 150}}).pretty()

Text Description automatically generated

Using $exists with $nin Operator : The $nin operator can also be used with $exists operator and these operators work in sequential manner as given below:

– First, $exists will select the documents based on the specified field:

– Then, $nin helps to print the documents that does not contain the specified values:

For instance, the following command will print the documents based on “designation” field; the staff having other than “Team-lead” designation will fall in this query:

> db.staff.find({designation: {$exists: true, $nin: ["Team-lead"]}}).pretty()

Text Description automatically generated

Similarly, various more comparison operators can also be practiced with the $exists command to get more refined output.

Conclusion

The querying feature of any database management system has a key role in retrieving data. As large-scale organizations have complex forms of data stored in their database; so, the companies prefer to apply queries to retrieve the required data within a time limit. The operators are the key component of any query; In this article, we have practiced the use of the $exists operator in MongoDB. This operator can be used to check the availability of fields in documents and you can get those documents that do not contain the specified field. The above-said functionality of the $exists operator is supported by a “Boolean-value” that can be passed to the operator.

About the author

Adnan Shabbir