aggregate pipeline for the breakdown that doesn’t fit the helpers.
Esix features used: count, sum, average, percentile, chained
where(...).sum(...), and the raw aggregate pipeline.
What You’ll Build
The Order Model
placedAt and fulfilledAt are unix milliseconds — easy to filter against
and easy to serialise from a JSON request.
The Metrics Endpoint
Define the query-string contract with zod, then compute all five metrics in parallel withPromise.all. Each helper is its own MongoDB round-trip, so
doing them concurrently keeps the endpoint snappy:
inRange()is a factory, not a stored query. Each call returns a freshQueryBuilder. Reusing a single instance acrosscount()andsum()would not work — once a terminal method runs the builder is consumed.- Aggregates return
0on empty results. No need for null checks on the numeric metrics. - Filtering chains naturally into aggregates. The p95 line filters out unfulfilled orders before computing the percentile.
Grouped Breakdowns with aggregate
count / sum / average are perfect for single numbers, but the top
categories list needs a $group stage. Drop down to MongoDB’s pipeline:
aggregate is the escape hatch when the chained helpers don’t cover what you
need. Keep the pipeline beside the model rather than inside the route handler
so the handler keeps reading like a summary, not a query plan.
Trying It Out
Pattern Notes
- Five round trips beats one cluttered pipeline. When each metric is its
own simple call, the code reads top-to-bottom and individual metrics are
easy to test in isolation. Reach for a single big
$facetonly when latency numbers tell you to. - Percentiles need a clean dataset. Filter out rows that don’t have the
measured field (
fulfilledAt !== null) before callingpercentile, otherwise you’ll pollute the distribution with zeroes. - Type the pipeline result.
Order.aggregate<CategoryRow>(...)keeps the raw MongoDB shape — including_id— under your control.
What’s Next
- Atomic Counters — increment metrics in real time instead of recomputing.
- Retrieving Models — Aggregate Functions — full
reference for
count,sum,average,min,max, andpercentile.