Query Engine API: 一括操作
In most cases you should not use the Query Engine API and rather use the Document Service API.
Only use the Query Engine API if you exactly know what you are doing, for instance if you want to use a lower-level API that directly interacts with unique rows of the database.
Please keep in mind that the Query Engine API is not aware of the most advanced Strapi 5 features like Draft & Publish, Internationalization, Content History, and possibly more.
This also means that the Query Engine API will not be able to use documentId and will use id, which means it could lead to unattended consequences at the database level or partial or incomplete compatibility with Strapi 5 features.
Before diving deeper into the Query Engine API documentation, it is recommended that you read the following introductions:
- the backend customization introduction,
- and the Content APIs introduction.
パフォーマンス上の理由から、リレーションに対する一括操作はできません。
createMany()
複数エントリを作成します。
構文: createMany(parameters) => { count: number, ids: id[] }
パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
data | オブジェクトの配列 | 入力データの配列 |
- MySQL では、挿入されたすべての id ではなく、最後に挿入された id だけが返る配列になります。
- Strapi v4.9.0 より前の
createMany()はcountだけを返します。
例
await strapi.db.query("api::blog.article").createMany({
data: [
{
title: "ABCD",
},
{
title: "EFGH",
},
],
});
// { count: 2 , ids: [1,2]}
updateMany()
条件に一致する複数エントリを更新します。
構文: updateMany(parameters) => { count: number }
パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
where | WhereParameter | 使う フィルター |
data | オブジェクト | 入力データ |
例
await strapi.db.query("api::shop.article").updateMany({
where: {
price: 20,
},
data: {
price: 18,
},
});
// { count: 42 }
deleteMany()
条件に一致する複数エントリを削除します。
構文: deleteMany(parameters) => { count: number }
パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
where | WhereParameter | 使う フィルター |
例
await strapi.db.query("api::blog.article").deleteMany({
where: {
title: {
$startsWith: "v3",
},
},
});
// { count: 42 }
集計
count()
条件に一致するエ ントリ数を数えます。
構文: count(parameters) => number
パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
where | WhereParameter | 使う フィルター |
const count = await strapi.db.query("api::blog.article").count({
where: {
title: {
$startsWith: "v3",
},
},
});
// 12