- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 to determine whether a field exists in MongoDB?
You need to use $exists operator to determine whether a field exists in MongoDB. Let us first create a collection with documents
> db.determineFieldExistsDemo.insertOne({"ClientName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9eb245d628fa4220163b75") } > db.determineFieldExistsDemo.insertOne({"ClientName":"Larry","ClientAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9eb25cd628fa4220163b76") } > db.determineFieldExistsDemo.insertOne({"ClientName":"Mike","ClientCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9eb26fd628fa4220163b77") } > db.determineFieldExistsDemo.insertOne({"ClientName":"Sam","ClientAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9eb286d628fa4220163b78") }
Following is the query to display all documents from a collection with the help of find() method
> db.determineFieldExistsDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9eb245d628fa4220163b75"), "ClientName" : "John" } { "_id" : ObjectId("5c9eb25cd628fa4220163b76"), "ClientName" : "Larry", "ClientAge" : 23 } { "_id" : ObjectId("5c9eb26fd628fa4220163b77"), "ClientName" : "Mike", "ClientCountryName" : "UK" } { "_id" : ObjectId("5c9eb286d628fa4220163b78"), "ClientName" : "Sam", "ClientAge" : 24 }
Following is the query to determine whether a field exists
> db.determineFieldExistsDemo.find({ClientCountryName:{$exists:true}}).pretty();
This will produce the following output
{ "_id" : ObjectId("5c9eb26fd628fa4220163b77"), "ClientName" : "Mike", "ClientCountryName" : "UK" }
- Related Articles
- How can I check whether a field exists or not in MongoDB?
- How to apply a condition only if field exists in MongoDB?
- Check that Field Exists with MongoDB?
- How to use Boto3 and AWS Resource to determine whether a root bucket exists in S3?
- How to use Boto3 and AWS Client to determine whether a root bucket exists in S3?
- Check if value exists for a field in a MongoDB document?
- How to select a field corresponding to the field in which MAX() exists?
- How do I check whether a field contains null value in MongoDB?
- Check whether field exist in MongoDB or not?
- How to increment a field in MongoDB?
- Determine if a Preference Node exists in Java
- How to select a single field in MongoDB?
- How to make a unique field in MongoDB?
- How to insert a boolean field in MongoDB?
- How to check whether a data frame exists or not in R?

Advertisements