使用查询引擎 API 进行过滤
¥Filtering with the Query Engine API
在大多数情况下,你不应使用查询引擎 API,而应使用 文档服务 API。
¥In most cases you should not use the Query Engine API and rather use the Document Service API.
仅当你确切知道自己在做什么时才使用查询引擎 API,例如,如果你想使用直接与数据库的唯一行交互的底层 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.
请记住,查询引擎 API 不了解最高级的 Strapi 5 功能,如草稿和发布、国际化、内容历史记录等。
¥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.
在深入了解查询引擎 API 文档之前,建议你阅读以下介绍:
¥Before diving deeper into the Query Engine API documentation, it is recommended that you read the following introductions:
-
和 内容 API 介绍。
¥and the Content APIs introduction.
查询引擎 API 能够过滤通过 findMany() 方法找到的结果。
¥The Query Engine API offers the ability to filter results found with its findMany() method.
结果使用接受 逻辑运算符 和 属性运算符 的 where
参数进行过滤。每个运算符都应以 $
为前缀。
¥Results are filtered with the where
parameter that accepts logical operators and attribute operators. Every operator should be prefixed with $
.
有关如何使用各种 API 进行深度过滤的示例,请参阅 这篇博客文章。
¥For examples of how to deep filter with the various APIs, please refer to this blog article.
逻辑运算符
¥Logical operators
$and
所有嵌套条件必须为 true
。
¥All nested conditions must be true
.
示例
¥Example
const entries = await strapi.db.query('api::article.article').findMany({
where: {
$and: [
{
title: 'Hello World',
},
{
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
],
},
});
当传递具有嵌套条件的对象时,隐式使用 $and
:
¥$and
is used implicitly when passing an object with nested conditions:
const entries = await strapi.db.query('api::article.article').findMany({
where: {
title: 'Hello World',
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
});
$or
一个或多个嵌套条件必须为 true
。
¥One or many nested conditions must be true
.
示例
¥Example
const entries = await strapi.db.query('api::article.article').findMany({
where: {
$or: [
{
title: 'Hello World',
},
{
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
],
},
});
$not
否定嵌套条件。
¥Negates the nested conditions.