Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Prisma · Module

count

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.

Since
2.15.0

How it works

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.

Syntax

prisma.<model>.count(args?)

Parameters

NameTypeDescription
optionsCountArgs<Model>Optional query arguments, including where filters and field selection for counting non-null values.

Returns

A number representing the count of matching records or non-null selected field values.

Key points

  • Use count() for either a model's records or non-null values of a field.
  • Calling prisma.user.count() without arguments counts every User record.
  • Supply where conditions to count only records satisfying a filter.
  • A numeric filter such as gte can restrict the counted records by profile views.
  • Filtering by a foreign-key field lets you count a particular user's related posts.

Common mistakes

  • A field-based count excludes records where that field is null.

Example: Count filtered records

const userCount = await prisma.user.count({
  where: { profileViews: { gte: 100 } }
});

Counts users whose profile views are at least 100.

When to use it

Model totals

Show the total number of records for a model.

Filtered counts

Measure a subset such as users above a profile-view threshold.

Related-record counts

Count records belonging to one related entity using a foreign-key filter.

Interview questions

QHow do you count records with Prisma Client?

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 } } }).

QHow do you count only records matching a condition?

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 }.

QWhat can Prisma's count operation count?

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()
  • _count
  • where
  • select

Source: Aggregation, grouping, and summarizing. Checked 2026-10-16.