Table of contents
1.
Introduction
2.
MongoDB Exists
3.
Syntax
3.1.
Parameters
4.
Examples
4.1.
With Specified Field
4.2.
Without Specified Field
4.3.
Find Documents Where a Nested Field Exists
4.4.
Combining $exists with Other Operators
4.5.
Using $exists in an Update Query
5.
Frequently Asked Questions
5.1.
What’s the purpose in MongoDB of the $exists operator?
5.2.
What’s the syntax of the $exists, operator?
5.3.
Can the $exists operator be used in combination with other operators?
5.4.
Can we use the $exists operator with nested fields in MongoDB?
5.5.
Does the $exists operator work with partial matching in MongoDB?
6.
Conclusion
Last Updated: Jul 9, 2024
Easy

MongoDB $exists

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey learners, the word "exists" means that something is present. With the help of the $exists operator, we can check whether a document is present or not. With the help of the $exists operator, we can easily retrieve and filter our documents based on the presence or absence of specific fields.

MongoDB Exists

Let's try to learn the use of the $exists operator and how we can use this in our daily life.

MongoDB Exists

The '$exists' operator in MongoDB is a handy way to check whether a specified field exists in any document or not. When it shows the true, it means that the particular document is present, and when the $exists operator value is set to the false, this returns nothing.

Syntax

{field: {$exists: <bool_value>}}

Parameters

  1. "field" shows what field you are trying to find in a collection.
     
  2. $exists is the operator which takes the boolean value (true or false).
    • True returns all documents where the given field exists.
    • False returns documents where the given field does not exist.

Examples

We’ll do all the operations on the below student collection. 

db.student.insertMany([
	{ id: 1, name: 'Abhi', gender: 'M', age:20 },
	{ id: 2, name: 'Pooja', gender: 'F', age:18},
	{ id: 3, name: 'Ryan', gender: 'M', age:21 },
	{ id: 4, regno: 210243, gender: 'M', age: 16}
]);


Output

Output

With Specified Field

In this, we use the $exists operator to find the data where the 'name' field exists. We are using the below query to find the data collection where the 'name' field exists.

db.student.find({name: {$exists: true}});


Student Collection

db.student.insertMany([
	{ id: 1, name: 'Abhi', gender: 'M', age:20 },
	{ id: 2, name: 'Pooja', gender: 'F', age:18},
	{ id: 3, name: 'Ryan', gender: 'M', age:21 },
	{ id: 4, regno: 210243, gender: 'M', age: 16}
]);

db.student.find({name: {$exists: true}});


Output

Output

Explanation

Above, we are using the $exists operator to select the documents from the “student” collection where the “name” field exists.

We can also find the existing field with a condition. So now let's find the documents whose "age" is greater than 18 and whose name exists. Below is the query we use to find the documents with a given condition.

db.student.find({name: {$exists: true}, age:{$gt:18}});


Student Collection

db.student.insertMany([
	{ id: 1, name: 'Abhi', gender: 'M', age:20 },
	{ id: 2, name: 'Pooja', gender: 'F', age:18},
	{ id: 3, name: 'Ryan', gender: 'M', age:21 },
	{ id: 4, regno: 210243, gender: 'M', age: 16}
]);

db.student.find({name: {$exists: true}, age:{$gt:18}});


Output

Output

Explanation

Above, we found all those documents whose names and ages are greater than 18 exist in the student collection.

Without Specified Field

Using the $exists operator, we can also find the data where the 'name' field doesn't exist. Below is the query to find the collection of data where the 'name' field doesn't exist.

db.student.find({name: {$exists: false}});


Student Collection

db.student.insertMany([
	{ id: 1, name: 'Abhi', gender: 'M', age:20 },
	{ id: 2, name: 'Pooja', gender: 'F', age:18},
	{ id: 3, name: 'Ryan', gender: 'M', age:21 },
	{ id: 4, regno: 210243, gender: 'M', age: 16}
]);

db.student.find({name: {$exists: false}});


Output

Output

Explanation

Here we find all those documents whose names don't exist in the student collection.

Find Documents Where a Nested Field Exists

If you have a nested field, such as address.city, and you want to find documents where city exists within address:

db.users.find({ "address.city": { $exists: true } })

This query returns all users who have a city field within their address field.

Combining $exists with Other Operators

You can combine $exists with other query operators to refine your search. For example, to find users who have an email field and their age is greater than 25:

db.users.find({ email: { $exists: true }, age: { $gt: 25 } })

This query returns all users who have an email field and whose age is greater than 25.

Using $exists in an Update Query

You can also use the $exists operator in update queries. For example, to add a new field verified: true to all users who have an email field:

db.users.updateMany(
 { email: { $exists: true } },
 { $set: { verified: true } }
)

This query updates all users who have an email field, adding the verified field set to true.

Frequently Asked Questions

What’s the purpose in MongoDB of the $exists operator?

The $exists operator in MongoDB checks the existence or absence of a specific field in a document.

What’s the syntax of the $exists, operator?

The syntax of $exists operator is {field: {$exists: <boolean> } }.

Can the $exists operator be used in combination with other operators?

Yeah, you can use the $exists operator with other query operators to create more complex and targeted searches.

Can we use the $exists operator with nested fields in MongoDB?

Yes, the $exists operator can be used with nested fields. The field path can include dot notation to specify nested fields within a document.

Does the $exists operator work with partial matching in MongoDB?

No, the $exists operator does not work with partial matching. It only checks for the existence or absence of fields.

Conclusion

In this article, we discussed the $exists operator and learned how to use this operator to check whether the document exists with the given field or not. We hope this blog helped you to enhance your knowledge of the $exists operator in MongoDB. Check the below articles to know more.

And many more on our platform Coding Ninjas Studio.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your coding ability, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For that purpose, take a look at the problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass