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