
- 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 do I check whether a field contains null value in MongoDB?
You can use $type operator to check whether a filed contains null value. Let us first create a collection with documents. We have also inserted a null in a field −
> db.nullDemo.insertOne({"FirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cc68a1eac184d684e3fa270") } > db.nullDemo.insertOne({"FirstName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5cc68a25ac184d684e3fa271") } > db.nullDemo.insertOne({"FirstName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cc68a2cac184d684e3fa272") }
Following is the query to display all documents from a collection with the help of find() method −
> db.nullDemo.find().pretty();
This will produce the following output. One of the field is null −
{ "_id" : ObjectId("5cc68a1eac184d684e3fa270"), "FirstName" : "Chris" } { "_id" : ObjectId("5cc68a25ac184d684e3fa271"), "FirstName" : null } { "_id" : ObjectId("5cc68a2cac184d684e3fa272"), "FirstName" : "Robert" }
Here is the query to check whether a field contains null value. The field “FirstName” is checked −
> db.nullDemo.find( { FirstName: { $type: 10 } } );
This will produce the following output −
{ "_id" : ObjectId("5cc68a25ac184d684e3fa271"), "FirstName" : null }
- Related Articles
- How can I check whether a field exists or not in MongoDB?
- Check if a field contains a string in MongoDB?
- Check whether a field is empty or null in MySQL?
- How do I update NULL values in a field in MySQL?
- How do I insert a NULL value in MySQL?
- Check whether field exist in MongoDB or not?
- How do I check for null values in JavaScript?
- How do we check in Python whether a string contains only numbers?
- How do you check if a field is NOT NULL with Eloquent?
- How do I add a field to existing record in MongoDB?
- How to check whether column value is NULL or having DEFAULT value in MySQL?
- How do I check whether a checkbox is checked in jQuery?
- How do I check whether a file exists using Python?
- Check if value exists for a field in a MongoDB document?
- How do I check if a column is empty or null in MySQL?

Advertisements