updateMany
Update every record matching a filter
Update every record matching the filter, in one call:
const result = await client.user.updateMany({
where: { role: "GUEST" },
data: { role: "USER" },
});
// Result: { count: 42 }
updateMany returns a { count }. where is optional — omit it to update every row.
Examples
// Archive old drafts
await client.post.updateMany({
where: {
createdAt: { lt: new Date("2023-01-01") },
published: false,
},
data: { archived: true },
});
// Apply a discount with an atomic update
await client.product.updateMany({
where: { category: "electronics" },
data: {
price: { multiply: 0.9 }, // 10% off
},
});
Getting the rows back
Add a select and updateMany returns the updated rows instead of a count. The
return type follows: with select it is an array of the projected rows, without
it, { count }.
const updated = await client.post.updateMany({
where: { published: false },
data: { published: true },
select: { id: true, title: true },
});
// Type: { id: string; title: string }[]
The rows are read after the update, so an atomic operation on the primary key returns the new identity, not the old one.
Capping how many rows are updated
limit caps the number of rows the update affects. The count you get back is
min(matching, limit):
// Retry at most 100 stuck jobs per tick
const result = await client.job.updateMany({
where: { status: "STUCK" },
data: { status: "PENDING" },
limit: 100,
});
// Result: { count: 100 } if at least 100 matched, fewer otherwise
limit: 0 is legal and means “update nothing”: you get { count: 0 } and no
write statement is sent at all. A negative or fractional limit is a validation
error.
limit also caps the returning form — select gives you back exactly the rows
that were updated, so at most limit of them.
Options
await client.user.updateMany({
where: { ... }, // Optional: filter (all rows if empty)
data: { ... }, // Required: fields to update
limit: 100, // Optional: cap on how many rows are updated
select: { ... }, // Optional (scalar fields only): return the updated rows
// instead of { count }
});