MongoDB Exists

Learn via video courses
Topics Covered

Overview

MongoDB is a popular NoSQL database that is used in modern web applications for its flexibility and scalability. When we have to find the documents which contain a specific field we use the MongoDB exists operator. Exists MongoDB operator can also be used to find documents that do not contain a specific field. If multiple documents match the MongoDB exists query criteria, all such documents are returned. If no document exists in MongoDB that matches the criteria then nothing is returned. $exists MongoDB operator can also be used for nested fields.

MongoDB $exists Query

  • The $exists MongoDB operator is an efficient way to check whether a specified field exists in any document.
  • If multiple documents match the criteria, all of them are returned.
  • If no document exists in MongoDB which matches the criteria, nothing is returned.

Syntax

  • Here "field" is the name of the field that we want to check for existence in the collection. We have to replace "field" with the name of the field that we want to query.

  • If MongoDB exists operator takes the boolean value “true” as an argument then it returns documents where the specified field exists, including documents where the field value is "null".

  • If Exists MongoDB operator takes the boolean value “false” as an argument then it returns the documents where the specified field does not exist.

  • The $exists MongoDB operator can also be used to check if a nested field exists.

  • Here "embeddedField" is the name of the nested field within the field "field" that we want to check for existence in the collection. We have to replace "embeddedField" with the name of the nested field that we want to query.

  • For example, in the document given below, "crew" is a field, and "director" and "producer" are two nested field inside "crew". syntax-of-mongodb-exists-query

  • Therefore, to check if the nested field "producer" exists the above syntax can be written as:

Methods

a. Check if the Field Exists:

Syntax:

Explanation:

This method checks if the field exists in the collection. All the documents that contain the field name are returned.

b. Check if the Embedded Field Exists:

  • Embedded fields are fields that are nested inside a field or an object in a document.
  • $exists MongoDB operator can also be used to check if an embedded field exists.
  • In the query, we will have to specify the entire path to the specified embedded field.

Syntax:

Explanation:

MongoDB exists operator is used for determining whether the embedded field exists inside the field in any document in the collection. If any document exists in MongoDB which satisfies the MongoDB exists query criteria, it is returned. If multiple documents satisfy the criteria, all of them are returned.

Examples

Example - 1:

We have a collection named "movies" with 6 documents. Only 3 out of 6 documents have a field named "crew".

example-of-use-of-mongodb-exists

example-of-use-of-mongodb-exists-1

A. We want to find all the documents having the field named "title" using the Exists MongoDB operator.

Syntax:

code-syntax-of-example-1-a

code-syntax-of-example-1-a-1

code-syntax-of-example-1-a-2

Therefore, we can see that all 6 documents have been returned by $exists MongoDB operator since all of them contain the field "title".


B. We will find the documents containing the field named "crew".

Syntax:

code-syntax-of-example-1-b

code-syntax-of-example-1-b-1

Therefore, we can see that 3 documents have been returned by the MongoDB exists operator, since they contain the field "crew".


C. Now we will find the documents which do not contain the field named "crew".

Syntax:

code-syntax-of-example-c

After MongoDB exists query has been executed we can see that 3 documents have been returned since they do not contain the field "crew".

Example - 2:

Now we will find the documents which have the embedded field "director" inside the field "crew".

Syntax:

code-syntax-of-example-2-a

code-syntax-of-example-2-a-1

Therefore, we can see that 3 documents have been returned, since they contain the embedded field "director" inside the field "crew".

FAQs

Q: Does the Exists MongoDB operator support all MongoDB data types?

A: Yes, $exists MongoDB operator supports all MongoDB data types.

Q: Is MongoDB exists operator case-sensitive?

A: No, MongoDB exists query is not case-sensitive. We can use either "false" or "False" and it will work the same.

Q: Can MongoDB exists query be used with regular expressions? A: No, MongoDB Exists operator works only with field names, not with regular expressions.

Conclusion

  • $exists MongoDB operator is used to return all the documents containing a specified field.
  • It can be used to find documents containing a particular field or not containing a particular field.
  • If multiple documents match the specified criteria, then all those documents are returned. If none of the documents match the specified criteria, then nothing is returned.
  • We can also use the Exists MongoDB operator for nested fields.