Good Place. Good Things. Good Time.

MongoDB exists

MongoDB exists

Definition of MongoDB exists

MongoDB provides different types of functionality to the user, the MongoDB exists as one of the functionalities provided by MongoDB. Basically, MongoDB provides a different comparison operator to the user, $exists is one of the comparison operators, when we set the $exists operator in the field of collection that means the $exists operator is true. After that $exists operator compares all the documents from the collection including the null field. That means as per our requirement we can set the $exists operator in the collection and get the desired result. When the set $exists operator is false then it returns the documents that do not match with the set value.

Syntax:

db.specified collection name.find({specified field name:{$exists:true}})

Explanation

In the above syntax, we use a comparison operator that is $exists with different parameters as follows.
specified collection name: Specified collection name means actual collection name that is already created by the user.
find(): It is a method and used to implement a comparison operator.
specified field name: specified field name means actual field name from the collection.
After that, we set the comparison operator that is $exists is true as shown.

How exists works in MongoDB?

Now let’s see how the $exists operator works in MongoDB as follows.

First, try to understand what the operator is. MongoDB provides different kinds of operators such as comparison, logical, element array and bitwise, etc. But we commonly used comparison operators among the different operators. Again we have different types of comparison operators such as eq, gt, lt, and gte, etc, and this operator we can use as per our requirement.

Normally the “not equal” operator is used to compare the specified value with all documents from the collection and if it finds any matching document from the collection then it displays all documents from the collection.
The working of the “not equal” operator is simple; we just need to mention the specified field name with the specified value that we want. The specified field name and specified value depend on the user. At the time of execution, it compares all specified values with all documents from the collection and if the value of the document is not equal to the specified value then it displays specified records.

Basically, the $exists operator is used to compare the field values that we need which means as per user requirement we can display the document from the collection. If we set $exists is true then it displays the matched field value that we already set and if we set $exist is false then it shows all fields that do not contain the field value that we already set from the collection. In the event that we make an association between collection and assortment, we see likenesses between lines – reports and sections – fields. The greatest contrast is that each archive from the same assortment can contain altogether different arrangements of fields. So the field can contain invalid, yet it might likewise not be there. Now and then, we need to separate between those. The method of doing checks additionally relies upon the utilization and what would you like to accomplish.

Basically “not null” constraint is used to display the document without null from the collection as per our requirement means as per our requirement we can choose any field name with a different comparison operator and Boolean operator. Basically, we can use the $exists a method to implement the not null in MongoDB. When is valid, $exists coordinates with the records that contain the field, including reports where the field esteem is invalid. In case is bogus, the question returns just the records that don’t contain the field.

Examples

Now let’s see different examples of the $exists comparison operator in the MongoDB for better understanding as follows.

  • First, we need to create the new collection but before that, we need to create the new database by using the following statement as follows.
  • First, we created a sample database by using the following statement as follows.

use sample

Explanation

In the above statement, we use the command to create the new database, here we successfully created a sample database and we use it. The end result or we can say the output of the above statement we illustrated by using the following screenshot as follows.

example 1

After successful creation of the database, we need to create the collection by using the following statement as follows.

db.createCollection(“student_sample”)

Explanation

In the above statement, we use create collection command to create the new collection; here we created a new collection name as a student_sample as shown. The end result or we can say the output of the above statement we illustrated by using the following screenshot as follows.

example 2

Now we have a new collection, so now we need to insert the different document into the newly created collection that is student_sample with a null field so we can get the result. For insertion of the document, we can use the following statement as follows.

db.student_sample.insert({ name: "Johan", dept: "IT", marks: 75, dbsm:56})
db.student_sample.insert({ name: "Sameer", dept: "comp", marks: 12, dbsm:null})
db.student_sample.insert({ name: "pooja", dept: "comp", marks: 45, dbsm:null})
db.student_sample.insert({ name: "Rohit" dept: "mech", marks: 71, dbsm:87})
db.student_sample.insert({ name: "Sachin", dept: "IT", marks: 98, dbsm:null})

Explanation

By using the above statement we insert a document into the student_sample collection, in which we insert names of students, department of the student, and dbsm marks with the null field as shown. Now we can see all documents from the collection by using the following statement as follows.

db.student_sample.find()

Explanation

The end result or we can say the output of the above statement we illustrated by using the following screenshot as follows.

example 3

Now we have a collection with documents now implementing the $exists as follows.

db.student_sample.find({dbsm:{$exists: true}})

Explanation

In the above example, we use a comparison operator that is $exists as shown, here we set the field value that dbsm with true. The dbsm contains a null value. The end result or we can say the output of the above statement we illustrated by using the following screenshot as follows.

example 4

So similarly we can implement $exists with false as per user requirement as follow here we just need to write the false instead of true.

db.student_sample.find({dbsm:{$exists: false}})

Conclusion

We hope from this article you learn more about MongoDB exists. From the above article, we have learned the basic syntax of $exists and we also see different examples of exists. From this article, we learned how and when we use MongoDB exists.

Recommended Articles

This is a guide to MongoDB exists. Here we discuss the definition, syntax, How exists works in MongoDB? examples with code implementation. You may also have a look at the following articles to learn more –

  1. MongoDB Delete Collection
  2. MongoDB group by
  3. MongoDB Database
  4. MongoDB Import

The post MongoDB exists appeared first on EDUCBA.

Read original article here: MongoDB exists

Read original article here: MongoDB exists



Disclaimers and Denial of responsibility..!

Denial of responsibility! TheTopMag.com is an automatic aggregator of all media around the world. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – jamesonline999@gmail.com . The content will be deleted within 72 hours.
You might also like
Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.