The $ifNull operator supplies a default value when a field is null or missing. Use it to clean up reports, fill gaps in user profiles, and avoid null checks in application code.
01
Null Fallback
Default when null/missing.
02
Syntax
Two-element array form.
03
Missing Fields
Treats absent keys as null.
04
$project Stage
Add safe computed fields.
05
Use Cases
Defaults, cleanup, reports.
06
Chaining
Try multiple fallbacks.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $ifNull operator evaluates an expression and returns it when the value is present. If the expression is null or the field is missing, MongoDB returns a replacement default instead. For example, show "Guest" when a user has no displayName.
💡
Beginner Tip
$ifNull only checks for null and missing fields. It does not treat 0, false, or "" as null — use $cond when you need those rules.
Foundation
📝 Syntax
$ifNull takes exactly two expressions in an array. The second value is the fallback:
The second argument does not have to be a literal — it can be any expression. MongoDB evaluates the fallback only when the first value is null or missing.
Applications
🚀 Use Cases
Default value assignment — fill null or missing fields with sensible placeholders in reports.
Data cleanup — normalize inconsistent documents before exporting or displaying.
User profiles — show display names, avatars, or contact info with chained fallbacks.
Financial reporting — treat null discounts, fees, or totals as zero or computed values.
🧠 How $ifNull Works
1
MongoDB evaluates the first expression
It reads a field reference or nested expression for the current document.
Input
2
$ifNull checks for null or missing
If the value exists and is not null, that value is returned immediately.
Check
3
Otherwise the replacement is used
The second expression — literal, field, or nested operator — becomes the result.
Fallback
=
🔄
Safe field value per document
Reports and APIs get consistent data without null-handling logic in your app.
Wrap Up
Conclusion
The $ifNull operator is the simplest way to handle null and missing values inside MongoDB aggregation pipelines. It keeps reports readable and reduces defensive coding in your application layer.
Start with single-field defaults, then chain nested $ifNull calls when you need multiple fallbacks. For boolean conditions (not just null checks), use $cond instead.
Use $ifNull for null and missing fields — it is shorter than equivalent $cond
Chain nested $ifNull for nickname → name → default patterns
Keep fallback types consistent (string with string, number with number)
Test documents with missing keys, explicit null, and real values
Combine with $project or $addFields for clean output fields
❌ Don’t
Expect $ifNull to replace 0, false, or ""
Use $cond when a simple null fallback is enough
Forget that only two arguments are allowed per $ifNull call
Assume nested fallbacks run in parallel — they evaluate left to right
Store large default objects in every pipeline — consider $let for reuse
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $ifNull
Use these points when handling null or missing values in MongoDB.
5
Core concepts
🔄01
Null Fallback
Default when null/missing.
Purpose
📝02
[ expr, default ]
Two-element array.
Syntax
📏03
Missing = null
Absent fields count.
Rule
🛠04
Chain Nested
Multiple fallbacks.
Pattern
⚠05
Not for 0 / ""
Use $cond instead.
Important
❓ Frequently Asked Questions
$ifNull returns the first expression when it is not null and not missing. If the first value is null or the field does not exist, it returns the second expression as a default.
The syntax is { $ifNull: [ <expression>, <replacement-if-null> ] }. The second value is used only when the first evaluates to null or is missing.
No. $ifNull only handles null and missing fields. Empty strings (""), 0, and false are real values and are returned as-is.
$ifNull is a shortcut for null/missing fallbacks. $cond evaluates any boolean condition — use it when you need comparisons like score >= 70.
Yes. Nest $ifNull in the replacement expression: { $ifNull: [ "$a", { $ifNull: [ "$b", "default" ] } ] } tries $a, then $b, then the final default.