Skip to main content
Version: 3.0 Beta

Aggregate

The aggregate method allows you to conduct multiple aggregations on a set of records with one operation. The supported aggregations are:

  • _count - equivalent to the Count API.
  • _sum - sum of a numeric field.
  • _avg - average of a numeric field.
  • _min - minimum value of a field.
  • _max - maximum value of a field.

You can also use where, orderBy, skip, and take to control what records are included in the aggregation.

Samples​

Click here to open an interactive playground.
aggregate.ts
import { createClient } from './db';
import { createUsersAndPosts } from './utils';

async function main() {
const db = await createClient();
await createUsersAndPosts(db);

console.log(
await db.post.aggregate({
where: { published: false },
// you can also use `count: true` to simply count all rows
_count: { _all: true, content: true },
_avg: { viewCount: true },
_max: { viewCount: true },
})
);
}

main();
Comments
Feel free to ask questions, give feedback, or report issues.

Don't Spam


You can edit/delete your comments by going directly to the discussion, clicking on the 'comments' link below