- 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
Check whether field exist in MongoDB or not?
You can use $exists operator for this. Let us first create a collection with documents −
>db.checkFieldExistsDemo.insertOne({"StudentFirstName":"John","StudentGender":"Male","StudentMongoDBScore":89}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909611a844af18acdffbd") } >db.checkFieldExistsDemo.insertOne({"StudentFirstName":"Emma","StudentGender":"Female","StudentMongoDBScore":58}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909781a844af18acdffbe") } >db.checkFieldExistsDemo.insertOne({"StudentFirstName":"Carol","StudentGender":"Male","StudentMongoDBScore":77}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909871a844af18acdffbf") } >db.checkFieldExistsDemo.insertOne({"StudentFirstName":"David","StudentMongoDBScore":98}); { "acknowledged" : true, "insertedId" : ObjectId("5cd909a31a844af18acdffc0") }
Following is the query to display all documents from a collection with the help of find() method −
> db.checkFieldExistsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd909611a844af18acdffbd"), "StudentFirstName" : "John", "StudentGender" : "Male", "StudentMongoDBScore" : 89 } { "_id" : ObjectId("5cd909781a844af18acdffbe"), "StudentFirstName" : "Emma", "StudentGender" : "Female", "StudentMongoDBScore" : 58 } { "_id" : ObjectId("5cd909871a844af18acdffbf"), "StudentFirstName" : "Carol", "StudentGender" : "Male", "StudentMongoDBScore" : 77 } { "_id" : ObjectId("5cd909a31a844af18acdffc0"), "StudentFirstName" : "David", "StudentMongoDBScore" : 98 }
Following is the query to check whether a field exist in MongoDB −
> db.checkFieldExistsDemo.find({"StudentMongoDBScore":98, "StudentGender":{"$exists": false}},{'StudentFirstName': 1, '_id':0});
This will produce the following output −
{ "StudentFirstName" : "David" }
- Related Articles
- How can I check whether a field exists or not in MongoDB?
- How to check whether a key exist in JavaScript object or not?
- Select MongoDB documents where a field either does not exist, is null, or is false?
- Check whether IdentityHashMap empty or not in Java?
- Check whether a field is empty or null in MySQL?
- How to check whether a particular key exist in javascript object or array?
- How do I check whether a field contains null value in MongoDB?
- How to check if a field in MongoDB is [] or {}?
- Check whether a NavigableMap empty or not in Java
- Check whether a Stack is empty or not in Java
- Check whether a HashSet is empty or not in Java
- Check whether a character is Lowercase or not in Java
- Check whether a character is Uppercase or not in Java
- Check whether the file is hidden or not in Java
- JavaScript Check whether string1 ends with strings2 or not

Advertisements