

- 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
Check that Field Exists with MongoDB?
You can use the $exists and $ne operator for this. To understand the concept further, let us create a collection with document. The query to create a collection with document is as follows −
> db.checkFieldExistDemo.insertOne({"EmployeeId":1,"EmployeeName":"John","isMarried":true,"EmployeeSalary":4648585}); { "acknowledged" : true, "insertedId" : ObjectId("5c76f7b31e9c5dd6f1f78281") } > db.checkFieldExistDemo.insertOne({"StudentId":2,"StudentName":"John","isMarried":false," StudentAge":19}); { "acknowledged" : true,0 "insertedId" : ObjectId("5c76f7e11e9c5dd6f1f78282") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.checkFieldExistDemo.find().pretty();
Output
{ "_id" : ObjectId("5c76f7b31e9c5dd6f1f78281"), "EmployeeId" : 1, "EmployeeName" : "John", "isMarried" : true, "EmployeeSalary" : 4648585 } { "_id" : ObjectId("5c76f7e11e9c5dd6f1f78282"), "StudentId" : 2, "StudentName" : "John", "isMarried" : false, "StudentAge" : 19 }
Here is the query to check whether the field exist in MongoDB.
Case 1 − When a field is present in more than one document. The query is as follows −
> db.checkFieldExistDemo.find({"isMarried":{$exists:true,$ne:null}}).pretty();
Output
{ "_id" : ObjectId("5c76f7b31e9c5dd6f1f78281"), "EmployeeId" : 1, "EmployeeName" : "John", "isMarried" : true, "EmployeeSalary" : 4648585 } { "_id" : ObjectId("5c76f7e11e9c5dd6f1f78282"), "StudentId" : 2, "StudentName" : "John", "isMarried" : false, "StudentAge" : 19 }
Case 2 − When a field is present in only one document. The query is as follows −
> db.checkFieldExistDemo.find({"StudentName":{$exists:true,$ne:null}}).pretty();
Output
{ "_id" : ObjectId("5c76f7e11e9c5dd6f1f78282"), "StudentId" : 2, "StudentName" : "John", "isMarried" : false, "StudentAge" : 19 }
- Related Questions & Answers
- Check if value exists for a field in a MongoDB document?
- Check if MongoDB database exists?
- How can I check whether a field exists or not in MongoDB?
- Check that a table exists in MySQL?
- How to determine whether a field exists in MongoDB?
- Find MongoDB documents that contains specific field?
- How to apply a condition only if field exists in MongoDB?
- Find documents that contains specific field in MongoDB?
- Check whether field exist in MongoDB or not?
- Check if a field contains a string in MongoDB?
- How to check empty field in a MongoDB collection?
- Implementing MongoDB $exists and $ne?
- How do I find all documents with a field that is NaN in MongoDB?
- Add field that is unique index to collection in MongoDB?
- MongoDB query to match documents that contain an array field
Advertisements