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.
Query Engine API では、findMany() の結果をフィルタリングできます。
where パラメータで結果を絞り込みます。where は 論理演算子 と 属性演算子 を受け取り、各演算子は $ で始まります。
For examples of how to deep filter with the various APIs, please refer to this blog article.
論理演算子
$and
ネストした条件がすべて true である必要があります。
例
const entries = await strapi.db.query('api::article.article').findMany({
where: {
$and: [
{
title: 'Hello World',
},
{
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
],
},
});
ネストした条件をオブジェクトで渡すと、暗黙的に $and が使われます。
const entries = await strapi.db.query('api::article.article').findMany({
where: {
title: 'Hello World',
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
});
$or
ネストした条件の 1 つ以上が true である必要があります。
例
const entries = await strapi.db.query('api::article.article').findMany({
where: {
$or: [
{
title: 'Hello World',
},
{
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
],
},
});
$not
ネストした条件を 否定します。
例
const entries = await strapi.db.query('api::article.article').findMany({
where: {
$not: {
title: 'Hello World',
},
},
});
$not は次のように使えます。
- 論理演算子(例:
where: { $not: { // conditions… }}) - 属性演算子(例:
where: { attribute-name: $not: { … } })。
$and、$or、$not は互いにネストできます。
属性演算子
これらの演算子は Strapi ではなくデータベース側で比較されるため、利用するデータベース実装により結果が異なる場合があります。
$not
ネストした条件を否定します。
例
const entries = await strapi.db.query('api::article.article').findMany({
where: {
title: {
$not: {
$contains: 'Hello World',
},
},
},
});