Skip to content

Commit

Permalink
fix(aggregation-grouping-summarizing): Improve count() (#5667)
Browse files Browse the repository at this point in the history
* fix(aggregation-grouping-summarizing): Improve count()

- Add headline for main "Count Records"
- More standard version related information as admonition
- Small changes

* Update content/200-orm/200-prisma-client/100-queries/056-aggregation-grouping-summarizing.mdx

---------

Co-authored-by: Jon Harrell <[email protected]>
  • Loading branch information
janpio and jharrell authored Mar 10, 2024
1 parent 015b215 commit a6d310e
Showing 1 changed file with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,20 +329,19 @@ Both `distinct` and `groupBy` group records by one or more unique field values.

## Count

Use [`count`](/orm/reference/prisma-client-reference#count) to count the number of records or non-`null` field values. The following example query counts all users:
### Count records

Use [`count()`](/orm/reference/prisma-client-reference#count) to count the number of records or non-`null` field values. The following example query counts all users:

```ts
const userCount = await prisma.user.count()
```

### Count relations

The ability to count relations is available in version [3.0.1](https://github.com/prisma/prisma/releases/3.0.1) and later.

<Admonition type="info">

**For versions before 3.0.1**<br />
You need to add the [preview feature](/orm/reference/preview-features/client-preview-features#enabling-a-prisma-client-preview-feature) `selectRelationCount` and then run `prisma generate`.
This feature is generally available in version [3.0.1](https://github.com/prisma/prisma/releases/3.0.1) and later. To use this feature in versions before 3.0.1 the [Preview feature](/orm/reference/preview-features/client-preview-features#enabling-a-prisma-client-preview-feature) `selectRelationCount` will need to be enabled.

</Admonition>

Expand Down Expand Up @@ -380,7 +379,7 @@ The `_count` parameter:
- Can be used inside a top-level `include` _or_ `select`
- Can be used with any query that returns records (including `delete`, `update`, and `findFirst`)
- Can return [multiple relation counts](#return-multiple-relation-counts)
- From version 4.3.0, can [filter relation counts](#filter-the-relation-count)
- Can [filter relation counts](#filter-the-relation-count) (from version 4.3.0)

#### Return a relations count with <inlineCode>include</inlineCode>

Expand Down Expand Up @@ -415,7 +414,7 @@ const usersWithCount = await prisma.user.findMany({

#### Return a relations count with `select`

The following query uses `select` to return each user's post count and no other fields:
The following query uses `select` to return each user's post count _and no other fields_:

<CodeWithResult>
<cmd>
Expand Down

0 comments on commit a6d310e

Please sign in to comment.