$bucket

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

Reference group: Pipeline stages

What it does

Groups values into explicit ranges.

Syntax

{$bucket: { groupBy, boundaries, default?, output? }}

Use case

Price bands and score ranges.

$bucket: practical example
{ $bucket: { groupBy: "$total", boundaries: [0, 100, 500, 1000], default: "1000+", output: { count: { $sum: 1 } } } }
💡 Explanation Groups values into explicit ranges. When to use it Price bands and score ranges.
Key Takeaways
  • Groups values into explicit ranges.
  • Typical use: Price bands and score ranges.