In MongoDB, the $exists operator is used to query documents that contain a specific field or property. The $exists operator evaluates to true if the specified field exists in the document, even if its value is null or an empty string. And, the $exists operator evaluates to false if the specified field does not exist in the document.
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: “james@gmail.com”,
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: “alex@gmail.com”,
marks: 90,
course: [“Python”, “PHP”]
}
]
)
2. MongoDB $exists operator with a true value
The mongoDB $exists operator is used to find documents that contain a specific field or property. The $exists operator evaluates to true if the specified field exists in the document, even if its value is null or an empty string. And, the $exists operator evaluates to false if the specified field does not exist in the document.
# $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 non-null 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 for embedded field
Although the $exists operator can be used to determine the existence of the given field of the embedded 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.
The output yielded below represents the document where the city field exists in the embedded field personal document.
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. Take a look below for this kind of implementation.
# exists operator with $nin operator
db.student.find( { marks: { $exists: true, $nin: [ 70, 75 ] } } )
According to the query filter where the marks field is set with $exits and the $nin operator determines only documents having a marks field that is not null and that does not have a value of 70 or 75 should be retrieved. 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.
Notice that the output obtained only displayed the document which fulfills the query filter criteria.
5. $exists operator with the $gt operator
Similarly, the $exists operator also be utilized with the conditional operator to check the existence of the field according to the specified value. There, we have provided the $exists and $gt operator query.
# $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.
The following output displayed the documents with the field age and is in the range specified to them.
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.
# $exists operator with the false value
db.student.find( { course: { $exists: false } } )
The defined query filter here fetched only those documents whose course field should be nonexistent or null. The $exist operator called there is used to examine whether a field is present in the document, and the value false specifically indicates that a field should not exist.
The following output below has a single document where the course field does not exist.
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.
More details about this topic can be found here.
In MongoDB, the $exists operator is used to query documents that contain a specific field or property. The $exists operator evaluates to true if the specified field exists in the document, even if its value is null or an empty string. And, the $exists operator evaluates to false if the specified field does not exist Read More MongoDB