
- 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 use $ifNull with MongoDB aggregation?
The $ifNull evaluates an expression and returns the value of the expression if the expression evaluates to a non-null value.
Let us first create a collection with documents −
> db.demo372.insertOne({"FirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e591aea2ae06a1609a00af6") } > db.demo372.insertOne({"FirstName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5e591aef2ae06a1609a00af7") } > db.demo372.insertOne({"FirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e591af42ae06a1609a00af8") } > db.demo372.insertOne({"FirstName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5e591afb2ae06a1609a00af9") }
Display all documents from a collection with the help of find() method −
> db.demo372.find();
This will produce the following output −
{ "_id" : ObjectId("5e591aea2ae06a1609a00af6"), "FirstName" : "Chris" } { "_id" : ObjectId("5e591aef2ae06a1609a00af7"), "FirstName" : null } { "_id" : ObjectId("5e591af42ae06a1609a00af8"), "FirstName" : "David" } { "_id" : ObjectId("5e591afb2ae06a1609a00af9"), "FirstName" : null }
Following is the query to use $ifNull with aggregation−
> db.demo372.aggregate( ... [ ... { ... $project: { ... ... FirstName: { $ifNull: [ "$FirstName", "NOT PROVIDED" ] } ... } ... } ... ] ... )
This will produce the following output −
{ "_id" : ObjectId("5e591aea2ae06a1609a00af6"), "FirstName" : "Chris" } { "_id" : ObjectId("5e591aef2ae06a1609a00af7"), "FirstName" : "NOT PROVIDED" } { "_id" : ObjectId("5e591af42ae06a1609a00af8"), "FirstName" : "David" } { "_id" : ObjectId("5e591afb2ae06a1609a00af9"), "FirstName" : "NOT PROVIDED" }
- Related Articles
- MongoDB aggregation with multiple keys
- MongoDB query to replace value with aggregation?
- How to compare two fields in aggregation filter with MongoDB?
- Perform min/max with MongoDB aggregation
- MongoDB aggregation with equality inside array?
- Can we use IFNULL along with MySQL ORDER BY?
- MongoDB aggregation to sum product price with similar IDs
- MongoDB aggregation to fetch documents with specific field value?
- Get Absolute value with MongoDB aggregation framework?
- MongoDB aggregation framework with group query example?
- Count by multiple fields with MongoDB aggregation
- How to Use Go With MongoDB?
- MongoDB aggregation to get two documents with the least marks
- Working with Aggregation to match all the values in MongoDB
- How to group nested fields in MongoDB aggregation with count value in array?

Advertisements