Model totals
Show the total number of records for a model.
Prisma · Module
Prisma Client's count() returns the number of records matching optional filters, or counts non-null values for a selected field. Use it for model totals and filtered record counts.
Prisma Client's count() method returns the number of records matching an optional where filter. It can count every record or, when a field is selected, only records whose field value is not null.
prisma.<model>.count(args?)| Name | Type | Description |
|---|---|---|
options | CountArgs<Model> | Optional query arguments, including where filters and field selection for counting non-null values. |
A number representing the count of matching records or non-null selected field values.
const userCount = await prisma.user.count({
where: { profileViews: { gte: 100 } }
});Counts users whose profile views are at least 100.
Show the total number of records for a model.
Measure a subset such as users above a profile-view threshold.
Count records belonging to one related entity using a foreign-key filter.
Call the model delegate's count() method, such as prisma.user.count(), to obtain the number of matching records. Add where to restrict which records are included.
Follow-up: How would you count only users with at least 100 profile views? Use prisma.user.count({ where: { profileViews: { gte: 100 } } }).
Pass a where object to count() containing the required conditions. Prisma applies those conditions before returning the count.
Follow-up: How would you count posts for one author? Filter with where: { authorId: theId }.
The method can count all records or non-null values for a selected field. The returned value is the count of records or qualifying field values.
Follow-up: What happens when no records match? The count is zero.
aggregate()groupBy()_countwhereselect