

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I check whether a field exists or not in MongoDB?
To check whether a field exists or not in MongoDB, you can use the $exists operator.
To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.checkFieldExistsOrNotDemo.insertOne({"StudentName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c92ba4136de59bd9de063a1") } > db.checkFieldExistsOrNotDemo.insertOne({"StudentName":"John","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c92ba4e36de59bd9de063a2") } > db.checkFieldExistsOrNotDemo.insertOne({"StudentName":"Chris","StudentAge":24,"StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c92ba6536de59bd9de063a3") } > db.checkFieldExistsOrNotDemo.insertOne({"StudentName":"Robert","StudentAge":21,"StudentCountryName":"UK","StudentHobby":["Teaching","Photography"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c92ba9d36de59bd9de063a4") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.checkFieldExistsOrNotDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c92ba4136de59bd9de063a1"), "StudentName" : "Larry" } { "_id" : ObjectId("5c92ba4e36de59bd9de063a2"), "StudentName" : "John", "StudentAge" : 21 } { "_id" : ObjectId("5c92ba6536de59bd9de063a3"), "StudentName" : "Chris", "StudentAge" : 24, "StudentCountryName" : "US" } { "_id" : ObjectId("5c92ba9d36de59bd9de063a4"), "StudentName" : "Robert", "StudentAge" : 21, "StudentCountryName" : "UK", "StudentHobby" : [ "Teaching", "Photography" ] }
Here is the query to check whether a field exists or not in MongoDB.
Case 1 − When a field exists.
The query is as follows −
> db.checkFieldExistsOrNotDemo.find({ 'StudentHobby' : { '$exists' : true }}).pretty();
The following is the output −
{ "_id" : ObjectId("5c92ba9d36de59bd9de063a4"), "StudentName" : "Robert", "StudentAge" : 21, "StudentCountryName" : "UK", "StudentHobby" : [ "Teaching", "Photography" ] }
Case 2 − When a field does not exist.
The query is as follows:
> db.checkFieldExistsOrNotDemo.find({ 'StudentTechnicalSubject' : { '$exists' : true }}).pretty();
You will not get anything when a field does not exist.
- Related Questions & Answers
- Check whether field exist in MongoDB or not?
- How to determine whether a field exists in MongoDB?
- C# Program to check whether a directory exists or not
- Java Program to check whether a file exists or not
- How to check whether a data frame exists or not in R?
- How do I check whether a field contains null value in MongoDB?
- Check that Field Exists with MongoDB?
- How to use Boto3 to check whether a Glue Job exists or not?
- How do I check whether a file exists using Python?
- Check if value exists for a field in a MongoDB document?
- How can I know whether MySQL Server is alive or not?
- How to check if a file exists or not in Java?
- How to check if a file exists or not using Python?
- How to check if a field in MongoDB is [] or {}?
- Check whether a NavigableMap empty or not in Java
Advertisements