
- 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
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 Articles
- Check if value exists for a field in a MongoDB document?
- How can I check whether a field exists or not in MongoDB?
- Check if MongoDB database exists?
- How to determine whether a field exists in MongoDB?
- How to apply a condition only if field exists in MongoDB?
- Check that a table exists in MySQL?
- Check whether field exist in MongoDB or not?
- Find MongoDB documents that contains specific field?
- Check if a field contains a string in MongoDB?
- How to check empty field in a MongoDB collection?
- Find documents that contains specific field in MongoDB?
- How to check if field is a number in MongoDB?
- Check duplicates of certain field for inner array in MongoDB
- How to check if a field in MongoDB is [] or {}?
- Check if any alert exists using selenium with python.

Advertisements