# 使用查询引擎 API 进行排序和分页

> Source: https://strapi.nodejs.cn/cms/api/query-engine/order-pagination

🌐 Ordering and Paginating with the Query Engine API

查询引擎 API 支持通过 `orderBy` 参数对单个或多个属性进行结果排序，包括关联排序，并通过 `offset` 和 `limit` 参数进行分页。

🌐 The Query Engine API supports ordering results with the `orderBy` parameter on single or multiple attributes, including relational ordering, and pagination with `offset` and `limit` parameters.

:::caution

在大多数情况下，你不应该使用查询引擎 API，而应使用 [文档服务 API](/cms/api/document-service)。

🌐 In most cases you should not use the Query Engine API and rather use the [Document Service API](/cms/api/document-service).

仅当你确切知道自己在做什么时才使用查询引擎 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 的最新功能，如草稿与发布、国际化、内容历史记录等，可能还有更多功能。这也意味着查询引擎 API 将无法使用 `documentId`，而将使用 `id`，这可能会在数据库层面导致未预料的后果，或者与 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.
This also means that the Query Engine API will not be able to use `documentId` and will use `id`, which means it could lead to unattended consequences at the database level or partial or incomplete compatibility with Strapi 5 features. 

:::

:::prerequisites

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

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

- 关于[后端自定义介绍](/cms/backend-customization)，
- 以及 [内容 API 介绍](/cms/api/content-api)。

:::

[查询引擎 API](/cms/api/query-engine) 提供了[排序](#ordering)和[分页](#pagination)结果的功能。

🌐 The [Query Engine API](/cms/api/query-engine) offers the ability to [order](#ordering) and [paginate](#pagination) results.

## 排序 {#ordering}

🌐 Ordering

要对查询引擎返回的结果进行排序，请使用 `orderBy` 参数。结果可以基于[单个](#single)或[多个](#multiple)属性进行排序，也可以使用[关系排序](#relational-ordering)。

🌐 To order results returned by the Query Engine, use the `orderBy` parameter. Results can be ordered based on a [single](#single) or on [multiple](#multiple) attributes and can also use [relational ordering](#relational-ordering).

### 单身 {#single}

🌐 Single

```js
strapi.db.query('api::article.article').findMany({
  orderBy: 'id',
});

// single with direction
strapi.db.query('api::article.article').findMany({
  orderBy: { id: 'asc' },
});
```

### 多个 {#multiple}

🌐 Multiple

```js
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}

🌐 Relational ordering

```js
strapi.db.query('api::article.article').findMany({
  orderBy: {
    author: {
      name: 'asc',
    },
  },
});
```

## 分页 {#pagination}

🌐 Pagination

要对查询引擎 API 返回的结果进行分页，请使用 `offset` 和 `limit` 参数：

🌐 To paginate results returned by the Query Engine API, use the `offset` and `limit` parameters:

```js
strapi.db.query('api::article.article').findMany({
  offset: 15, 
  limit: 10,
});
```
