$switch

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Conditional and logical expressions

What it does

Returns the first matching branch result.

Syntax

{$switch: { branches: [{ case, then }], default }}

Use case

Multi-range classification.

$switch: practical example
{ $project: { band: { $switch: { branches: [{ case: { $lt: ["$score", 50] }, then: "low" }, { case: { $lt: ["$score", 80] }, then: "medium" }], default: "high" } } } }
💡 Explanation Returns the first matching branch result. When to use it Multi-range classification.
Key Takeaways
  • Returns the first matching branch result.
  • Typical use: Multi-range classification.