
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
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 Articles
- Check whether field exist in MongoDB or not?
- How to determine whether a field exists in MongoDB?
- How to check whether a data frame exists or not in R?
- How do I check whether a field contains null value in MongoDB?
- C# Program to check whether a directory exists or not
- Java Program to check whether a file exists or not
- Check that Field Exists with MongoDB?
- How to use Boto3 to check whether a Glue Job exists or not?
- Check if value exists for a field in a MongoDB document?
- How do I check whether a file exists using Python?
- How to check if a field in MongoDB is [] or {}?
- How to check if a file exists or not in Java?
- How to check if a file exists or not using Python?
- How can I change the field name in MongoDB?
- How can I know whether MySQL Server is alive or not?

Advertisements