Queries
Reading Records
| Method | On no match | Description |
|---|---|---|
findMany | Returns [] | Read multiple records |
findFirst | Returns null | Read the first matching record |
findFirstOrThrow | Raises NOT_FOUND | Same, return type excludes null |
findUnique | Returns null | Read by a unique key |
findUniqueOrThrow | Raises NOT_FOUND | Same, return type excludes null |
findManyAndCount | data is [] | Return the list and the total together |
count | Returns 0 | Count matching records |
const posts = await db.post.findMany({
where: { published: true },
orderBy: { id: "desc" },
take: 20,
});
const user = await db.user.findUnique({ where: { email: "alice@example.com" } });
const total = await db.post.count({ where: { published: true } });2
3
4
5
6
7
8
findUnique needs a complete unique key: the full primary key (all @@id fields), an @unique field, or all fields of an @@unique.
Filters
await db.post.findMany({
where: {
title: { startsWith: "Hello" },
views: { gte: 10 },
body: null,
OR: [{ published: true }, { authorId: { in: [1, 2] } }],
},
});2
3
4
5
6
7
8
| Operator | Applicable types | Description |
|---|---|---|
equals | All except JSON | Equality, or a bare value |
not | All except JSON | Inequality, or negates a sub-condition |
in, notIn | All except JSON | In or not in a list |
gt, gte, lt, lte | string, text, string enums, numeric, datetime, timestamp | Comparison |
contains, startsWith, endsWith | string, text, string enums | String matching |
Conditions combine with AND, OR, and NOT, nested at most 12 levels.
Null and Empty Collections
| Form | Behavior |
|---|---|
field: null | Matches NULL; raises an error on a non-nullable field |
field: 'x' | NULL does not match |
{ not: 'x' } | Matches values not equal to x, and also matches NULL |
{ in: [] } | Matches no record |
{ notIn: [] } | Matches every record |
AND: [] | Always true |
OR: [] | Always false |
contains, startsWith, and endsWith use LIKE with automatic escaping; case sensitivity depends on the database and collation (see Remaining Differences). mode: 'insensitive' makes a string condition case-insensitive; PostgreSQL uses ILIKE for patterns, MySQL / MariaDB LOWER().
JSON Fields
JSON fields are filtered by path, where the path consists of object keys and array indices:
await db.account.findMany({
where: { preferences: { path: ["theme"], equals: "dark" } },
});
await db.account.findMany({
where: { preferences: { path: ["languages", 0], string_starts_with: "a" } },
});2
3
4
5
6
| Operator | Description |
|---|---|
equals, not | Equal or not equal to a string, number, boolean, or JSON null; differing types count as unequal |
gt, gte, lt, lte | Numeric comparison; non-numeric values never match |
string_contains, string_starts_with, string_ends_with | String matching; non-string values never match |
Use ordinary scalar fields for ordering, deduplication, and grouping.
Relation Filters
| Operator | Applicable relation | Description |
|---|---|---|
some | To-many | At least one matches |
none | To-many | None matches |
every | To-many | All match |
is, isNot | To-one | The target matches or not; may be null |
await db.user.findMany({
where: { posts: { some: { published: true } } },
});
await db.user.findMany({
where: { profile: { is: null } },
});2
3
4
5
6
7
Projections
select chooses returned fields, include attaches relations, omit removes fields; return types are inferred accordingly.
const users = await db.user.findMany({
select: {
email: true,
posts: { take: 3, orderBy: { id: "desc" }, select: { title: true } },
},
});
const posts = await db.post.findMany({
include: { author: { select: { email: true } } },
omit: { authorId: true },
});2
3
4
5
6
7
8
9
10
11
- Use
select, orincludewithomit;omitalso works in nested queries, and projections nest at most 8 levels. An omitted foreign key is still loaded internally to resolve relations. - Fields omitted via
$extend({ omit })are excluded by default; bring them back with query-levelselectoromit: { field: false }, see Extensions.
Relation Loading
To-many relations accept where, orderBy, skip, and take, with pagination computed per parent record:
await db.user.findMany({
include: {
posts: { where: { published: true }, orderBy: { views: "desc" }, take: 3 },
},
});2
3
4
5
To-one relations only accept select, include, omit, and deleted.
Relation Counts
_count in select or include counts without loading child records; no match gives 0.
const users = await db.user.findMany({
select: {
email: true,
_count: { select: { posts: { where: { published: true } } } },
},
});2
3
4
5
6
_count: true counts all to-many relations of the model.
Ordering
await db.post.findMany({
orderBy: [{ views: "desc" }, { id: "asc" }],
});2
3
- Without the primary key in the ordering fields, the ORM appends an ascending primary key for stable results.
NULLsorts as the smallest value by default (first ascending, last descending) on all three databases; use{ sort, nulls }(firstorlast) to place it explicitly — cursor pagination and negativetakehonor it:
await db.user.findMany({ orderBy: { name: { sort: "desc", nulls: "last" } } });When ordering by a relation, a to-one relation uses target-model fields, a to-many relation orders by count:
await db.post.findMany({ orderBy: { author: { email: "asc" } } });
await db.user.findMany({ orderBy: { posts: { _count: "desc" } } });2
Relation ordering runs through correlated subqueries, excludes soft-deleted targets, and cannot be combined with cursor.
Pagination
skip must be a non-negative integer. A top-level take may be negative: the last N records by orderBy, still returned in orderBy order:
await db.post.findMany({ orderBy: { id: "asc" }, take: -10 });take in a relation query must be a non-negative integer. A negative take cannot be combined with distinct.
Cursor Pagination
await db.post.findMany({
cursor: { id: 10 },
skip: 1,
take: 20,
orderBy: [{ views: "desc" }, { id: "asc" }],
});2
3
4
5
6
The result includes the cursor record itself (skip: 1 to pass it; a missing cursor returns an empty array). A negative take pages backwards from the cursor ("previous page"):
await db.post.findMany({
cursor: { id: 10 },
skip: 1,
take: -20,
orderBy: [{ views: "desc" }, { id: "asc" }],
});2
3
4
5
6
Deduplication
distinct picks the first record per group by orderBy (via window functions), then paginates:
await db.post.findMany({
distinct: ["authorId"],
orderBy: { views: "desc" },
});2
3
4
List and Total
const { data, total } = await db.account.findManyAndCount({
where: { email: { contains: "@example.com" } },
include: { entries: true },
orderBy: { id: "desc" },
skip: 0,
take: 20,
});2
3
4
5
6
7
total applies the same where, soft-delete visibility, and distinct; cursor, skip, take, and projections do not affect it. Standalone calls open a repeatable read read-only snapshot transaction and reuse an existing one, but raise TRANSACTION_ISOLATION at read committed, since the two reads could diverge. See Transactions.
Next Steps
- Aggregations
- Create, Update, and Delete