Available since 4.3.0.

enable in your schema file:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["filteredRelationCount"] << add this 
}

and then query:

await prisma.post.findMany({
  select: {
    _count: {
      select: {
        comment: { where: { approved: true } },
      },
    },
  },
})
Answer from eagor on Stack Overflow
🌐
Prisma
prisma.io › home › aggregation, grouping, and summarizing › aggregation, grouping, and summarizing › aggregation, grouping, and summarizing › aggregation, grouping, and summarizing
Aggregation, grouping, and summarizing (Concepts) | Prisma Documentation
You can do this on scalar fields and relation fields. For example, the following query returns all user posts with the title "Hello!": // Count all user posts with the title "Hello!" await prisma.user.findMany({ select: { _count: { select: { posts: { where: { title: 'Hello!'
Discussions

Prisma - filter by related records count
From the example docs : const result = await prisma.user.findMany({ where: { posts: { some: { views: { gt: 10, }, }, }, }, }) Extrapolating, could you try: const result = await prisma.applicants.findMany({ where: { familyMembers: { _count: { gt: 10, } }, }, }) See also: https://www.prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing#count-relations https://www.prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing#filter-the-relation-count More on reddit.com
🌐 r/node
2
4
November 16, 2023
How does one implement Prisma's features like count relations and nested reads?
Hello! I have been actively using Redwood for a week now for a hobby project and it has been a great learning experience for me as a general programming novice. The tutorial has been especially helpful! I have done a deep dive into Prisma’s docs and found some features I wanted to try. More on community.redwoodjs.com
🌐 community.redwoodjs.com
1
1
December 29, 2021
Support aggregations (e.g. count) on nested relations
Problem Right now if you want to just get the count of the model in the same query, it is not possible. Suggested solution Introduce an aggregate key in include/select for nested relation in additi... More on github.com
🌐 github.com
22
September 23, 2020
How do I get the count in nested relations.
const posts = await prisma.pos... image: true, publish: true, comments: { _count: { id: true } } } }); PrismaJoinThe official Discord server of Prisma! Find us online at prisma.io ... How to get the sum of a nested relations.... More on answeroverflow.com
🌐 answeroverflow.com
February 13, 2025
🌐
GitHub
github.com › prisma › prisma › issues › 2519
Count relationships · Issue #2519 · prisma/prisma
May 19, 2020 - Hi everyone! Yesterday i opened a discussion asking if it was possible to count relationships with my prisma client and still fetching data in the same query. @ryands17 answered me that prisma currently doesnt support it.
Author   prisma
🌐
Prisma
prisma.io › home › relation queries › relation queries › relation queries › relation queries
Relation queries (Concepts) | Prisma Documentation
Learn how to filter Prisma Client queries with where and sort results with orderBy. Nested readsRelation load strategies (Preview)ExamplesWhen to use which load strategy?Include a relationInclude all fields for a specific relationInclude deeply nested relationsSelect specific fields of included relationsRelation countFilter a list of relationsNested writesCreate a related recordCreate a single record and multiple related recordsUsing nested createUsing nested createManyCreate multiple records and multiple related recordsConnect multiple recordsConnect a single recordConnect or create a recordD
🌐
GitHub
github.com › prisma › prisma › issues › 5079
Support aggregations (e.g. count) on nested relations · Issue #5079 · prisma/prisma
September 23, 2020 - Right now if you want to just get the count of the model in the same query, it is not possible. Introduce an aggregate key in include/select for nested relation in addition to the where key · const users = await prisma.user.findMany({ include: { _count: { select: { posts: true, }, }, }, }) Doing a separate aggregate call for fetching the count.
Author   prisma
Find elsewhere
🌐
Answer Overflow
answeroverflow.com › m › 1339548074187685949
How do I get the count in nested relations. - Prisma
February 13, 2025 - const posts = await prisma.posts.findMany({ where: { userId, }, orderBy: [ { startDate: 'asc' }, ], select: { id: true, title: true, image: true, publish: true, comments: { _count: { id: true } } } }); PrismaJoinThe official Discord server of Prisma! Find us online at prisma.io ... How to get the sum of a nested relations.
🌐
DE JAY'S BLOG
dejaysblog.com › home › coding › how to get counts on relations with prisma client
How to get counts on relations with Prisma client
April 11, 2021 - But with the new previewFeatures option we can now view the number of counts on our relation types. ... 1. Include the previewFeatures option in your Prisma client and set its value as "selectRelationCount" as shown below.
🌐
YouTube
youtube.com › watch
Getting Counts on Relations with Prisma Client - YouTube
New at Prisma 2.20 is the ability to get the counts of related fields. This is useful for cases where you'd like to get the total number of records for a mod...
Published   April 1, 2021
Top answer
1 of 1
2

I figured out myself. This is a preview feature in prisma 2.30. To solve this, just upgrade both prisma and prisma/client version to 3.30 with

npm install prisma@3 @prisma/client@3 

Even though the count relations is added since prisma 2.21. But from there release note, this function is generally available in prisma 3.01. So, in 2.30, this function might not be available. To use it in prisma 2.30, the preview feature needs to be enabled.

Enabling a Prisma Client preview feature To enable a Prisma Client Preview feature:

Add the Preview feature flag to the generator block:

generator client { provider = "prisma-client-js"
previewFeatures = ["mongoDb"] } Re-generate the client:

npx prisma generate If you are using Visual Studio Code and the Preview feature is not available in your .ts file after generating the client, run the TypeScript: Restart TS server command.

Prisma 3.30 Release Notes

Select Relation Count is Generally Available Select Relation Count allows you to count the number of related records by passing _count to the select or include options and then specifying which relation counts should be included in the resulting objects via another select.

Select Relation Count helps you query counts on related models, for example, counting the number of posts per user:

const users = await prisma.user.findMany({ include: { _count: { select: { posts: true }, }, }, }) Expand to view the structure of the returned users If you've been using this Preview feature, you can remove the selectRelationCount flag from previewFeatures in your Prisma Schema.

See the upgrade reference here. See Prisma Release Notes here.

🌐
GitHub
github.com › prisma › prisma › issues › 8413
Ability to filter count in "Count Relation Feature" · Issue #8413 · prisma/prisma
July 23, 2021 - Currently count relation feature just accepts true or false values and count all the related records. Sometimes we just need to count some of the records depends on the query. ... this.prisma.groupExamAttempt.findMany({ where: { groupId: ...
Author   prisma
🌐
Stack Overflow
stackoverflow.com › questions › 72248573 › count-or-include-filtered-relations-prisma
Count or Include filtered relations prisma - Stack Overflow
return await prisma.asset.findMany({ take: parseInt(pageSize), skip: (pageSize * pageNumber), include: { _count: { select: { views: true }, }, views: { where: { createdAt: dateFilter }, }, likes: { where: { createdAt: dateFilter } }, transactions: true, }, orderBy: { views: { _count: 'desc' } }
🌐
GitHub
github.com › prisma › prisma › issues › 23949
Filter on count of relations · Issue #23949 · prisma/prisma
April 24, 2024 - kind/featureA request for a new ...uping-summarizing#count-https://www.prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing#count-topic: filteredRelationCounttopic: relations...
Author   prisma
🌐
Pothos-graphql
pothos-graphql.dev › docs › plugins › prisma › relations
Relations
Prisma supports querying for relation counts which allow including counts for relations along side other includes. Before prisma 4.2.0, this does not support any filters on the counts, but can give a total count for a relation.
🌐
GitHub
github.com › prisma › prisma › issues › 3821
Filter based on number / count of relations · Issue #3821 · prisma/prisma
September 30, 2020 - const paths = await prisma.locations.findMany({ where: { Products: { not: null // anything to show product relations greater than 0 } } })
Author   prisma