Skip to content
Justice Tech School

$facet, $bucket and $count

$bucket groups by ranges, $facet runs several pipelines on the same input at once and $count counts documents.

Example
db.products.aggregate([
  { $bucket: { groupBy: "$price", boundaries: [0, 50, 100, 500], default: "other" } }
])
db.products.aggregate([{ $count: "total" }])

Exercise

Ranges.

{ $: { groupBy: "$price" } }

Quiz Test

What is $facet for?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.