The $cond operator adds if-then-else logic inside MongoDB aggregation pipelines. Use it to label records, compute derived fields, or return different values based on a condition.
01
If-Then-Else
Branch on true/false.
02
Syntax
if, then, else keys.
03
Comparisons
Pair with $gte, $eq.
04
$project Stage
Add computed fields.
05
Use Cases
Grades, status, defaults.
06
Nested $cond
Multi-tier decisions.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $cond operator evaluates a boolean condition and returns one of two expressions. Think of it as an inline if-then-else statement inside your pipeline. For example, assign "Pass" when a score is at least 70, otherwise "Fail".
💡
Beginner Tip
The if branch must evaluate to a boolean (true or false). Use comparison operators like $gte, $eq, or $and to build your condition.
Foundation
📝 Syntax
$cond supports two equivalent forms. Beginners often find the object form easiest to read:
Each else branch contains another $cond, creating a decision tree. For many branches, consider $switch as a cleaner alternative.
Applications
🚀 Use Cases
Conditional aggregation — compute different values per document based on criteria.
Data transformation — categorize records, derive status labels, or map scores to grades.
Dynamic querying — return alternate field values inside reports without changing stored data.
Safe defaults — replace invalid or edge-case values with fallbacks during projection.
🧠 How $cond Works
1
MongoDB evaluates the if condition
Comparison or logical operators resolve to true or false for each document.
Condition
2
$cond picks a branch
True → then expression. False → else expression.
Branch
3
The result is stored in the pipeline
The chosen value becomes the new field value in $project or $addFields.
Output
=
🔄
Computed field per document
Each document gets its own result based on its data — no application code required.
Wrap Up
Conclusion
The $cond operator brings if-then-else logic directly into MongoDB aggregation pipelines. It is ideal for labeling, grading, status fields, and conditional transformations without post-processing in your application.
Start with simple pass/fail conditions, then explore nested $cond or $switch for multi-way branching. Pair with comparison operators like $gte and $eq to build clear, readable conditions.
Use the object form when learning; switch to array form when comfortable
Keep then and else the same type when possible
Use $switch when you have many branches
Test edge cases (exactly at threshold, null fields)
❌ Don’t
Put non-boolean values in the if branch
Nest too many $cond operators without considering $switch
Confuse $cond with $ifNull for general comparisons
Forget that $gte uses an array: ["$score", 70]
Assume pipeline order does not matter — conditions run per document in stage order
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $cond
Use these points when adding conditional logic in MongoDB.
5
Core concepts
🔄01
If-Then-Else
Pipeline branching.
Purpose
📝02
if / then / else
Object or array form.
Syntax
📏03
Boolean if
Use $gte, $eq, etc.
Rule
🛠04
Any Expression
then/else can nest ops.
Flexibility
⚠05
Not $ifNull
General conditions.
Important
❓ Frequently Asked Questions
$cond evaluates a boolean condition and returns one of two expressions — the then value when true, or the else value when false. It is MongoDB's if-then-else for aggregation pipelines.