
- 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 for null in MongoDB?
We will use the Null type here. Following are the null types with the alias −
Type | Number | Alias |
---|---|---|
Double | 1 | “double” |
String | 2 | “string” |
Object | 3 | “object” |
Array | 4 | “array” |
Binary data | 5 | “binData” |
Undefined | 6 | “undefined” |
ObjectId | 7 | “objectId” |
Boolean | 8 | “bool” |
Date | 9 | “date” |
Null | 10 | “null” |
Regular Expression | 11 | “regex” |
Following is the syntax for type 10 i.e. null −
db.yourCollectionName.find({"yourFieldName":{ $type: 10 } });
The above syntax will find only those documents which have null value. Let us first create a collection with documents −
> db.mongoDbEqualDemo.insertOne({"Age":34}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7e9121a844af18acdffa3") } > db.mongoDbEqualDemo.insertOne({"Age":""}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7e9161a844af18acdffa4") } > db.mongoDbEqualDemo.insertOne({"Age":null}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7e9191a844af18acdffa5") } > db.mongoDbEqualDemo.insertOne({"Age":56}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7e91e1a844af18acdffa6") } > db.mongoDbEqualDemo.insertOne({}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7e9261a844af18acdffa7") } > db.mongoDbEqualDemo.insertOne({"Age":null}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7e92e1a844af18acdffa8") }
Following is the query to display all documents from a collection with the help of find() method −
> db.mongoDbEqualDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd7e9121a844af18acdffa3"), "Age" : 34 } { "_id" : ObjectId("5cd7e9161a844af18acdffa4"), "Age" : "" } { "_id" : ObjectId("5cd7e9191a844af18acdffa5"), "Age" : null } { "_id" : ObjectId("5cd7e91e1a844af18acdffa6"), "Age" : 56 } { "_id" : ObjectId("5cd7e9261a844af18acdffa7") } { "_id" : ObjectId("5cd7e92e1a844af18acdffa8"), "Age" : null }
Following is the query to check for equal to null values only −
> db.mongoDbEqualDemo.find({"Age":{ $type: 10 } });
This will produce the following output −
{ "_id" : ObjectId("5cd7e9191a844af18acdffa5"), "Age" : null } { "_id" : ObjectId("5cd7e92e1a844af18acdffa8"), "Age" : null }
- Related Articles
- MongoDB checking for not null?
- Check for NULL or NOT NULL values in a column in MySQL
- Check for Existing Document in MongoDB?
- How do I check whether a field contains null value in MongoDB?
- How do I check for null values in JavaScript?
- Python Pandas – Check for Null values using notnull()
- How can we check for NULL in a MySQL query?
- Querying null value in MongoDB?
- Check for duplicates in an array in MongoDB?
- How to check for null, undefined, or blank variables in JavaScript?
- Check for NULL or empty variable in a MySQL stored procedure
- Check for existing documents/embedded documents in MongoDB
- Ignore null values in MongoDB documents
- Conditional $first in MongoDB aggregation ignoring NULL?
- C# Program to check a string for whitespace characters or null

Advertisements