Queries
count
Count records matching a filter with an efficient aggregate query.
Counts the number of records matching the where clause. Returns a number.
const total = await client.db.User.count();
// total: numbercount uses an efficient SELECT count() FROM ... GROUP ALL query — only the count is returned from the database, not the actual records. This makes it suitable for large tables where you need the total without fetching data.
Options
| Option | Type | Required | Description |
|---|---|---|---|
where | UserWhereInput | No | Filter conditions (omit to count all records) |
The where argument is passed directly — not wrapped in an options object.
Count All Records
const total = await client.db.User.count();
// total: number (e.g., 42)Count with Filter
const activeUsers = await client.db.User.count({ isActive: true });
// activeUsers: numberconst recentPosts = await client.db.Post.count({
createdAt: { gte: new Date('2024-01-01') },
});
// recentPosts: numberCombining with Pagination
Use count alongside findMany to build paginated responses:
const pageSize = 10;
const page = 1;
const where = { isActive: true };
const [users, total] = await Promise.all([
client.db.User.findMany({
where,
limit: pageSize,
offset: (page - 1) * pageSize,
orderBy: { createdAt: 'desc' },
}),
client.db.User.count(where),
]);
const totalPages = Math.ceil(total / pageSize);
// users: User[], total: number, totalPages: numbercount vs exists
When you only need to know whether any records match, prefer exists — it makes the intent clearer:
// Use count when you need the actual number
const adminCount = await client.db.User.count({ role: 'admin' });
console.log(`There are ${adminCount} admins`);
// Use exists when you only need a boolean
if (await client.db.User.exists({ role: 'admin' })) {
// at least one admin exists
}Both use the same efficient aggregate query under the hood.
Return Value
- Returns a
numberrepresenting the count of matching records. - Returns
0if no records match.