Skip to main content

Bulk Operations with the Query Engine API

🏗 Work in progress

The content of this page might not be fully up-to-date with Strapi 5 yet.

Caution

To avoid performance issues, bulk operations are not allowed on relations.

createMany()

Creates multiple entries.

Syntax: createMany(parameters) => { count: number, ids: id[] }

Parameters

ParameterTypeDescription
dataArray of objectsArray of input data
Caution
  • MySQL will only return an array of one id containing the last inserted id, not the entire list.
  • Prior to Strapi v4.9.0, createMany() only returns the count.

Example

await strapi.db.query("api::blog.article").createMany({
data: [
{
title: "ABCD",
},
{
title: "EFGH",
},
],
});

// { count: 2 , ids: [1,2]}

updateMany()

Updates multiple entries matching the parameters.

Syntax: updateMany(parameters) => { count: number }

Parameters

ParameterTypeDescription
whereWhereParameterFilters to use
dataObjectInput data

Example

await strapi.db.query("api::shop.article").updateMany({
where: {
price: 20,
},
data: {
price: 18,
},
});

// { count: 42 }

deleteMany()

Deletes multiple entries matching the parameters.

Syntax: deleteMany(parameters) => { count: number }

Parameters

ParameterTypeDescription
whereWhereParameterFilters to use

Example

await strapi.db.query("api::blog.article").deleteMany({
where: {
title: {
$startsWith: "v3",
},
},
});

// { count: 42 }

Aggregations

count()

Counts entries matching the parameters.

Syntax: count(parameters) => number

Parameters

ParameterTypeDescription
whereWhereParameterFilters to use
const count = await strapi.db.query("api::blog.article").count({
where: {
title: {
$startsWith: "v3",
},
},
});

// 12