Reference

db.aggregate

Grouped SQL aggregates (count/sum/avg/min/max/countDistinct) without fetching rows.
Query.aggregate({ by, select, orderBy, limit }) → row[]

Computes grouped aggregates — count, sum, avg, min, max, countDistinct — in the database with GROUP BY, returning one object per group rather than the rows themselves. Use it for totals and rollups over tables large enough that fetching every row to reduce in memory would be wasteful. For a single value over one column the .count, .sum, and .avg accessors read cleaner; drop to db.sql only when the shape is beyond what grouping can express.

Parameters
by
string[]
Group-by columns.
select
object
RequiredAggregate terms.
Aggregate
const top = await Answers.aggregate({
  by: ['questionId'],
  select: { n: { count: true }, avgScore: { avg: 'score' } },
});