
- 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 to query for records where field is null or not set in MongoDB?
Let us work around two cases −
Case 1 − The syntax is as follows when the field is present and set to null.
db.yourCollectionName.count({yourFieldName: null});
Case 1 − The syntax is as follows when the field is not present and not set.
db.yourCollectionName.count({yourFieldName: {$exists: false}});
To understand both the above syntaxes, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.fieldIsNullOrNotSetDemo.insertOne({"EmployeeName":"Larry","EmployeeAge":null,"EmployeeSalary":18500}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a995c6cea1f28b7aa07fe") } > db.fieldIsNullOrNotSetDemo.insertOne({"EmployeeName":"Bob","EmployeeAge":21,"EmployeeSalary":23500}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a99836cea1f28b7aa07ff") } > db.fieldIsNullOrNotSetDemo.insertOne({"EmployeeName":"Carol","EmployeeSalary":45500}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a999b6cea1f28b7aa0800") } > db.fieldIsNullOrNotSetDemo.insertOne({"EmployeeName":"Mike","EmployeeAge":null,"EmployeeSalary":45500}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a99bb6cea1f28b7aa0801") } > db.fieldIsNullOrNotSetDemo.insertOne({"EmployeeName":"Ramit","EmployeeSalary":85500}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a99d76cea1f28b7aa0802") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.fieldIsNullOrNotSetDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8a995c6cea1f28b7aa07fe"), "EmployeeName" : "Larry", "EmployeeAge" : null, "EmployeeSalary" : 18500 } { "_id" : ObjectId("5c8a99836cea1f28b7aa07ff"), "EmployeeName" : "Bob", "EmployeeAge" : 21, "EmployeeSalary" : 23500 } { "_id" : ObjectId("5c8a999b6cea1f28b7aa0800"), "EmployeeName" : "Carol", "EmployeeSalary" : 45500 } { "_id" : ObjectId("5c8a99bb6cea1f28b7aa0801"), "EmployeeName" : "Mike", "EmployeeAge" : null, "EmployeeSalary" : 45500 } { "_id" : ObjectId("5c8a99d76cea1f28b7aa0802"), "EmployeeName" : "Ramit", "EmployeeSalary" : 85500 }
Case 1 −
The field EmployeeAge is present and set to null.
The query is as follows −
> db.fieldIsNullOrNotSetDemo.count({EmployeeAge: null});
The following is the output −
4
Case 2 −
The field ‘EmployeeAge’ is not present and not set. The query is as follows −
> db.fieldIsNullOrNotSetDemo.count({EmployeeAge: {$exists: false}});
The following is the output −
2
- Related Articles
- Find MongoDB records where array field is not empty?
- Select MongoDB documents where a field either does not exist, is null, or is false?
- MongoDB query to select one field if the other is null and the first field if both are not null?
- MongoDB query which represents not equal to null or empty?
- Use NOT IN, OR and IS NULL in the same MySQL query to display filtered records
- MongoDB Query for boolean field as “not true”
- MongoDB query to select one field if the other is null?
- How does MongoDB Update() method work to set records of entire field?
- MongoDB checking for not null?
- MySQL query to set values for NULL occurrence
- MongoDB query for a single field
- MySQL query to display records on the basis of conditions IS NULL OR !=1;?
- How to query on list field in MongoDB?
- MongoDB Query to search for records only in a specific hour?
- MySQL query to remove Null Records in a column?

Advertisements