メインコンテンツまでスキップ
非公開のページ
このページは非公開です。 検索対象外となり、このページのリンクに直接アクセスできるユーザーのみに公開されます。

Query Engine API: 一括操作

Caution

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.

Prerequisites

Before diving deeper into the Query Engine API documentation, it is recommended that you read the following introductions:

Caution

パフォーマンス上の理由から、リレーションに対する一括操作はできません。

createMany()

複数エントリを作成します。

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

パラメータ

パラメータ説明
dataオブジェクトの配列入力データの配列
Caution
  • 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 }

パラメータ

パラメータ説明
whereWhereParameter使う フィルター
dataオブジェクト入力データ

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

// { count: 42 }

deleteMany()

条件に一致する複数エントリを削除します。

構文: deleteMany(parameters) => { count: number }

パラメータ

パラメータ説明
whereWhereParameter使う フィルター

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

// { count: 42 }

集計

count()

条件に一致するエントリ数を数えます。

構文: count(parameters) => number

パラメータ

パラメータ説明
whereWhereParameter使う フィルター
const count = await strapi.db.query("api::blog.article").count({
where: {
title: {
$startsWith: "v3",
},
},
});

// 12