文档服务 API:筛选器
🌐 Document Service API: Filters
文档服务 API 提供筛选结果的功能。
🌐 The Document Service API offers the ability to filter results.
可以使用以下运算符:
🌐 The following operators are available:
| 运算符 | 描述 |
|---|---|
$eq | 相等 |
$eqi | 相等(不区 分大小写) |
$ne | 不相等 |
$nei | 不等于(不区分大小写) |
$lt | 少于 |
$lte | 小于或等于 |
$gt | 大于 |
$gte | 大于或等于 |
$in | 包含在数组中 |
$notIn | 不包含在数组中 |
$contains | 包含 |
$notContains | 不包含 |
$containsi | 包含(不区分大小写) |
$notContainsi | 不包含(不区分大小写) |
$null | 为空 |
$notNull | 不为空 |
$between | 介于 |
$startsWith | 以...开始 |
$startsWithi | 以…开头(不区分大小写) |
$endsWith | 以...结尾 |
$endsWithi | 以…结尾(不区分大小写) |
$or | 将过滤器组合成“或”表达式 |
$and | 将过滤器组合成“与”表达式 |
$not | 在“not”表达式中连接过滤器 |
有关如何使用各种 API 进行深度过滤的示例,请参阅 this blog article。
属性运算符
🌐 Attribute operators
$not
否定嵌套条件。
🌐 Negates the nested condition(s).
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$not: {
$contains: 'Hello World',
},
},
},
});
$eq
属性等于输入值。
🌐 Attribute equals input value.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$eq: 'Hello World',
},
},
});
$eq可以省略:
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: 'Hello World',
},
});
$eqi
属性等于输入值(不区分大小写)。
🌐 Attribute equals input value (case-insensitive).
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$eqi: 'HELLO World',
},
},
});
$ne
属性不等于输入值。
🌐 Attribute does not equal input value.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$ne: 'ABCD',
},
},
});
$nei
属性不等于输入值(不区分大小写)。
🌐 Attribute does not equal input value (case-insensitive).
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$nei: 'abcd',
},
},
});
$in
属性包含在输入列表中。
🌐 Attribute is contained in the input list.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$in: ['Hello', 'Hola', 'Bonjour'],
},
},
});
在传递一个值数组时可以省略 $in:
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: ['Hello', 'Hola', 'Bonjour'],
},
});
$notIn
输入列表中不包含属性。
🌐 Attribute is not contained in the input list.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$notIn: ['Hello', 'Hola', 'Bonjour'],
},
},
});
$lt
属性小于输入值。
🌐 Attribute is less than the input value.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
rating: {
$lt: 10,
},
},
});
$lte
属性小于或等于输入值。
🌐 Attribute is less than or equal to the input value.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
rating: {
$lte: 10,
},
},
});
$gt
属性大于输入值。
🌐 Attribute is greater than the input value.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
rating: {
$gt: 5,
},
},
});
$gte
属性大于或等于输入值。
🌐 Attribute is greater than or equal to the input value.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
rating: {
$gte: 5,
},
},
});
$between
属性介于两个输入值之间,包括边界(例如,$between[1, 3] 也会返回 1 和 3)。
🌐 Attribute is between the 2 input values, boundaries included (e.g., $between[1, 3] will also return 1 and 3).
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
rating: {
$between: [1, 20],
},
},
});
$contains
属性包含输入值(区分大小写)。
🌐 Attribute contains the input value (case-sensitive).
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$contains: 'Hello',
},
},
});
$notContains
属性不包含输入值(区分大小写)。
🌐 Attribute does not contain the input value (case-sensitive).
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$notContains: 'Hello',
},
},
});
$containsi
属性包含输入值。$containsi不区分大小写,而$contains区分大小写。
🌐 Attribute contains the input value. $containsi is not case-sensitive, while $contains is.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$containsi: 'hello',
},
},
});
$notContainsi
属性不包含输入值。$notContainsi 不区分大小写,而 $notContains 区分大小写。
🌐 Attribute does not contain the input value. $notContainsi is not case-sensitive, while $notContains is.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$notContainsi: 'hello',
},
},
});
$startsWith
属性以输入值开头(区分大小写)。
🌐 Attribute starts with input value (case-sensitive).
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$startsWith: 'ABCD',
},
},
});
$startsWithi
属性以输入值开头(不区分大小写)。
🌐 Attribute starts with input value (case-insensitive).
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$startsWithi: 'ABCD', // will return the same as filtering with 'abcd'
},
},
});
$endsWith
属性以输入值结尾(区分大小写)。
🌐 Attribute ends with input value (case-sensitive).
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$endsWith: 'ABCD',
},
},
});
$endsWithi
属性以输入值结尾(不区分大小写)。
🌐 Attribute ends with input value (case-insensitive).
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$endsWith: 'ABCD', // will return the same as filtering with 'abcd'
},
},
},
});
$null
属性是 null。
🌐 Attribute is null.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$null: true,
},
},
});
$notNull
属性不是 null。
🌐 Attribute is not null.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$notNull: true,
},
},
});
逻辑运算符
🌐 Logical operators
$and
所有嵌套条件必须是 true。
🌐 All nested conditions must be true.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
$and: [
{
title: 'Hello World',
},
{
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
],
},
});
$and 在传递带有嵌套条件的对象时将被隐式使用:
const entries = await strapi.documents('api::article.article').findMany({
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.documents('api::article.article').findMany({
filters: {
$or: [
{
title: 'Hello World',
},
{
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
],
},
});
$not
否定嵌套条件。
🌐 Negates the nested conditions.
示例
const entries = await strapi.documents('api::article.article').findMany({
filters: {
$not: {
title: 'Hello World',
},
},
});
$not 可以用作:
- 一个逻辑运算符(例如在
filters: { $not: { // conditions… }}中) - 属性操作符(例如在
filters: { attribute-name: $not: { … } }中)。
$and、$or 和 $not 操作符可以嵌套在另一个 $and、$or 或 $not 操作符中。