使用查询引擎进行排序和分页 API
¥Ordering and Paginating 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 提供 order 和 paginate 结果的能力。
¥The Query Engine API offers the ability to order and paginate results.
顺序
¥Ordering
要对查询引擎返回的结果进行排序,请使用 orderBy
参数。结果可以基于 single 或 multiple 属性进行排序,也可以使用 关系排序。
¥To order results returned by the Query Engine, use the orderBy
parameter. Results can be ordered based on a single or on multiple attributes and can also use relational ordering.
单身的
¥Single
strapi.db.query('api::article.article').findMany({
orderBy: 'id',
});
// single with direction
strapi.db.query('api::article.article').findMany({
orderBy: { id: 'asc' },
});
多种的
¥Multiple
strapi.db.query('api::article.article').findMany({
orderBy: ['id', 'name'],
});
// multiple with direction
strapi.db.query('api::article.article').findMany({
orderBy: [{ title: 'asc' }, { publishedAt: 'desc' }],
});
关系排序
¥Relational ordering
strapi.db.query('api::article.article').findMany({
orderBy: {
author: {
name: 'asc',
},
},
});
分页
¥Pagination
要对查询引擎 API 返回的结果进行分页,请使用 offset
和 limit
参数:
¥To paginate results returned by the Query Engine API, use the offset
and limit
parameters:
strapi.db.query('api::article.article').findMany({
offset: 15,
limit: 10,
});