使用实体服务 API 进行筛选
🌐 Filtering with the Entity Service API
在 Strapi v5 中,实体服务 API 已被弃用。请考虑改用 文档服务 API。
🌐 The Entity Service API is deprecated in Strapi v5. Please consider using the Document Service API instead.
实体服务 API 提供了使用其 findMany() 方法筛选结果的功能。
🌐 The Entity Service API offers the ability to filter results found with its findMany() method.
结果通过 filters 参数进行过滤,该参数接受 逻辑运算符 和 属性运算符。每个运算符都应以 $ 为前缀。
🌐 Results are filtered with the filters parameter that accepts logical operators and attribute operators. Every operator should be prefixed with $.
有关如何使用各种 API 进行深度过滤的示例,请参阅 this blog article。
逻辑运算符
🌐 Logical operators
$and
所有嵌套条件必须是 true。
🌐 All nested conditions must be 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
一个或多个嵌套条件必须是 true。
🌐 One or many nested conditions must be true.
示例
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
$or: [
{
title: 'Hello World',
},
{
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
],
},
});
$not
否定嵌套条件。
🌐 Negates the nested conditions.
示例
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
$not: {
title: 'Hello World',
},
},
});
$not 可以用作:
- 一个逻辑运算符(例如在
filters: { $not: { // conditions… }}中) - 属性操作符(例如在
filters: { attribute-name: $not: { … } }中)。
$and、$or 和 $not 操作符可以嵌套在另一个 $and、$or 或 $not 操作符中。
属性运算符
🌐 Attribute Operators
根据数据库的实现,使用这些运算符可能会给出不同的结果,因为比较是由数据库而不是 Strapi 处理的。
🌐 Using these operators may give different results depending on the database's implementation, as the comparison is handled by the database and not by Strapi.
$not
否定嵌套条件。
🌐 Negates the nested condition(s).
示例
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: {
$not: {
$contains: 'Hello World',
},
},
},
});
$eq
属性等于输入值。
🌐 Attribute equals input value.
示例
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: {
$eq: 'Hello World',
},
},
});
$eq可以省略:
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: 'Hello World',
},
});
$eqi
属性等于输入值(不区分大小写)。
🌐 Attribute equals input value (case-insensitive).
示例
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: {
$eqi: 'HELLO World',
},
},
});
$ne
属性不等于输入值。
🌐 Attribute does not equal input value.
示例
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: {
$ne: 'ABCD',
},
},
});
$nei
属性不等于输入值(不区分大小写)。
🌐 Attribute does not equal input value (case-insensitive).