The $switch operator evaluates multiple case branches and returns the result from the first match — like a switch/case statement in programming. Use it instead of deeply nested $cond when you have three or more outcomes.
01
Multi-Branch
Many cases at once.
02
Syntax
branches + default.
03
First Match
Top-down order.
04
$project Stage
Label records.
05
Use Cases
Grades, tiers, status.
06
vs $cond
Switch vs if/else.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $switch operator checks an ordered list of case expressions. When a case evaluates to true, MongoDB returns the corresponding then value and stops. If no case matches, it returns the optional default value (or null if omitted).
This is ideal for letter grades, priority tiers, shipping categories, and any scenario where a single field maps to one of several labels based on numeric ranges or equality checks.
💡
Beginner Tip
Put the most specific or highest-priority cases first. MongoDB only uses the first branch that matches — later branches are skipped.
Foundation
📝 Syntax
The $switch operator uses a branches array and an optional default:
Include a default branch when you always need a fallback value.
Applications
🚀 Use Cases
Grading and scoring — map numeric scores to letter grades or performance bands.
Priority classification — tier customers, orders, or tickets by amount or urgency.
Status labeling — convert codes or enums to display-friendly strings.
Shipping and pricing — assign categories based on weight, distance, or order value.
🧠 How $switch Works
1
MongoDB reads the branches array
Each branch has a case (condition) and a then (result).
Input
2
Cases are tested top to bottom
The first branch whose case evaluates to true wins. Remaining branches are skipped.
Evaluate
3
The then value is returned
If no case matches, MongoDB returns default (or null).
Output
=
🔀
Multi-way label assigned
Use in $project or $addFields to enrich documents.
Wrap Up
Conclusion
The $switch operator simplifies multi-branch conditional logic in MongoDB aggregation pipelines. It replaces hard-to-read nested $cond chains with a flat list of cases and an optional default.
Order branches from most specific to least specific, and always include a default when you need a guaranteed fallback. Next in the series: $tan.
Use $switch when you have three or more possible outcomes
Order branches from highest threshold to lowest (for range checks)
Include a default branch for unexpected values
Build case expressions with $gte, $eq, or $and
Keep each branch focused on one clear condition
❌ Don’t
Put lower thresholds before higher ones in range-based grading
Forget that only the first matching branch runs
Use non-boolean expressions in case fields
Nested $cond deeply when $switch would be clearer
Assume default is required — omitting it returns null
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $switch
Use these points when building multi-way conditionals.
5
Core concepts
🔀01
Multi-Branch
Case list.
Purpose
📝02
branches[]
case + then.
Syntax
🔢03
First match
Top-down.
Rule
🛠04
default
Fallback value.
Optional
⚠05
vs $cond
Flat vs nested.
Compare
❓ Frequently Asked Questions
$switch evaluates an ordered list of case branches and returns the then value from the first branch whose case expression is true. If no branch matches, it returns the default value (or null if default is omitted).
The syntax is { $switch: { branches: [ { case: <expression>, then: <expression> }, ... ], default: <expression> } }. Branches are checked top to bottom; only the first match wins.
$cond handles one if-then-else decision. $switch handles many branches in one expression — like a switch/case statement. Use $switch instead of deeply nested $cond for readability.
No. default is optional. If you omit it and no case matches, $switch returns null.
Inside expression stages such as $project, $addFields, $set, and $group accumulators whenever you need multi-way conditional logic.