In MongoDB, the $exists
operator is used to query documents that contain a specific field or property. It takes boolean value true/false. When <boolean>
is true, it returns the documents that contain the field, including documents where the field value is null
. If <boolean>
is false, it returns only the documents that do not contain the field.
1. Syntax of $exists operator
Following is the syntax or usage of the $exists
operator.
# Syntax
{ field_name: { $exists: boolean_value } }
Where field_name
defines the field’s name to check, and boolean_value
is either true or false.
Let’s create the sample collection with a few documents to explain the $exists operator.
# Create Collection
db.student.insertMany([
{
_id: 1,
name: "James",
age: 25,
email: "[email protected]",
marks: 75,
course: ["SQL", "MongoDB"]
},
{
_id: 2,
name: "Elena",
age: 21,
marks: 85,
course: ["Java", "SQL"]
},
{
_id: 3,
name: "Emily",
age: 20,
marks: 70,
personal: {"phone#": 3627791, "city":"New York"}
},
{
_id: 4,
name: "Alex",
age: 23,
email: "[email protected]",
marks: 90,
course: ["Python", "PHP"]
}
]
)
2. MongoDB $exists – Get documents if the field exists or not exists
The $exists operator in MongoDB is used to retrieve the documents only if a field exists or not. Use value true, to get all documents that have the specified field. Use the value false to get the documents that have no specified field.
Let’s look at the example with the value true.
# $exists operator inside the find() method
db.student.find({email:{$exists:true}})
Here, the email field is set with the $exits
operator with the value true
. This specifies that only documents with a email
field should be returned. The $exists
operator here checks if a field is present in the document, and the value true
specifies that it should exist.
The output shows the document with the email field exists.
If you want to read about find() method using in ($in operator), use the following link.
3. MongoDB $exists operator with an embedded or nested field
The $exists
operator can also be used to retrieve the document by checking the existence of the given field in the embedded or nested document.
# $exists operator for embedded field
db.student.find({"personal.city":{$exists:true}})
The query filter provided above specifies that only documents with a non-null city
field inside a personal
subdocument should be returned by the $exits
operator. The value true
set to the $exist operator indicates that a field should exist when a field’s existence is being checked using the $exists
operator.
Moreover, the $exists
operator can be used with various other operators to satisfy the criteria. Here, the $exists
operator is called along with the $nin
operator. Here is an example.
# exists operator with $nin operator
db.student.find( { marks: { $exists: true, $nin: [ 70, 75 ] } } )
We retrieve documents with a marks
field exists and does not have a value of 70 or 75. Here, a field’s existence in the document is checked by the $exists
operator, and the inclusion of the value in a given array is determined by the $nin
operator.
Yields below output.
5. $exists operator with the $gt operator
Similarly, the MongoDB $exists
operator also be utilized with the conditional operator to check the existence of the field according to the specified value.
# $exists operator with the $gt operator
db.student.find({
age: {
$exists: true,
$gt: 21
}
});
Above, the query filter defines that only documents that have a age
field, not null, and have a value greater than 21 are to be returned. The purpose of the $exists
operator is to determine whether a field is present in the document, whereas the $gt
operator is used to determine if a value exceeds a predefined value.
Yields below output.
6. $exists operator with the false value
As we know, the $exists
operator sets with the boolean value. Here, we provide the false value to the $exists
operator to get the documents that provide the null field or field not exists.
# $exists operator with the false value
db.student.find( { course: { $exists: false } } )
Here, we fetched only those documents where the course
field does not exist or is null.
Yields below output.
7. Conclusion
In conclusion, the $exists operator is explored in this article with the syntax explanation and the implementation of the examples. Therefore, the $exists operator is useful in cases where we need to filter documents based on the existence or non-existence of a specific field.
When <boolean>
is true, it returns the documents that contain the field, including documents where the field value is null
. If <boolean>
is false, it returns only the documents that do not contain the field.
More details about this topic can be found here.
Related Articles
- MongoDB $elemMatch Operator Usage
- MongoDB $or Operator Usage
- MongoDB insertOne() & insertMany() Documents
- Upsert in MongoDB Explained with Examples
- MongoDB Realm
- Working with MongoDB Transactions
- MongoDB distinct() – Get Unique values from field
- MongoDB $match Usage
- MongoDB Delete Documents from Collection
- Install MongoDB On Ubuntu
- MongoDB $filter Operator Usage