Skip to content
Related Articles

Related Articles

MongoDB – Check the existence of the fields in the specified collection
  • Last Updated : 05 Feb, 2021
GeeksforGeeks - Summer Carnival Banner

In MongoDB, we can check the existence of the field in the specified collection using the $exists operator. When the value of $exists operator is set to true, then this operator matches the document that contains the specified field(including the documents where the value of that field is null). When the value of $exists operator is set to false, then this operator returns only those documents that don’t contain the specified field. 

Syntax:

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

Examples: 

In the following example, we are working with:



Database: gfg

Collections: student

Document: Three documents contains name and age of the students

  • Check the existence of the field in the student collection:
db.student.find({name:{$exists:true}})

Here, we check the field exists or not in the student collection using $exists operator.

  • Check the existence of the field of the embedded document:
db.student.find({"detials.game":{$exists:true}})

Here, we check the field of the embedded document is exists or not using the $exists operator.


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :