Skip to main content

查询引擎 API

¥Query Engine API

Strapi 后端提供了一个查询引擎 API 来与更底层的数据库层进行交互。

¥The Strapi backend provides a Query Engine API to interact with the database layer at a lower level.

提醒

在大多数情况下,你不应使用查询引擎 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.

☑️ Prerequisites

在深入了解查询引擎 API 文档之前,建议你阅读以下介绍:

¥Before diving deeper into the Query Engine API documentation, it is recommended that you read the following introductions:

基本用法

¥Basic usage

查询引擎可通过 strapi.db.query 获得:

¥The Query Engine is available through strapi.db.query:

strapi.db.query('api::blog.article').findMany({ // uid syntax: 'api::api-name.content-type-name'
where: {
title: {
$startsWith: '2021',
$endsWith: 'v4',
},
},
populate: {
category: true,
},
});

可用操作

¥Available operations

查询引擎允许对数据库条目执行以下操作:

¥The Query Engine allows the following operations on database entries: