Skip to main content

文档服务 API:结果的排序和分页

🌐 Document Service API: Sorting and paginating results

Page summary:

使用文档服务 API 的 sort 和分页参数按单个或多个字段排序查询结果,并使用 limit 和 start 控制结果的限制。

🌐 Use the Document Service API's sort and pagination parameters to order query results by single or multiple fields and control result limits with limit and start.

文档服务 API 提供对查询结果进行排序和分页的功能。

🌐 The Document Service API offers the ability to sort and paginate query results.

排序​

🌐 Sort

要对文档服务 API 返回的结果进行排序,请在查询中包含 sort 参数。

🌐 To sort results returned by the Document Service API, include the sort parameter with queries.

按单个字段排序​

🌐 Sort on a single field

strapi.documents().findMany()

按单个字段排序

使用字符串值按单个字段排序结果。

const documents = await strapi.documents("api::article.article").findMany({
sort: "title:asc",
});
200 OK
[
{
"documentId": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1"
},
{
"documentId": "cjld2cjxh0001qzrm5q1j5q7m",
"title": "Test Article 2",
"slug": "test-article-2",
"body": "Test 2"
}
]

按多个字段排序​

🌐 Sort on multiple fields

strapi.documents().findMany()

按多个字段排序

通过传入一组排序对象来对多个字段的结果进行排序。

const documents = await strapi.documents("api::article.article").findMany({
sort: [{ title: "asc" }, { slug: "desc" }],
});
200 OK
[
{
"documentId": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1"
},
{
"documentId": "cjld2cjxh0001qzrm5q1j5q7m",
"title": "Test Article 2",
"slug": "test-article-2",
"body": "Test 2"
}
]

分页​

🌐 Pagination

strapi.documents().findMany()

分页

使用 limit 和 start 参数来分页显示结果。

const documents = await strapi.documents("api::article.article").findMany({
limit: 10,
start: 0,
});
200 OK
[
{
"documentId": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1"
},
{
"documentId": "cjld2cjxh0001qzrm5q1j5q7m",
"title": "Test Article 2",
"slug": "test-article-2",
"body": "Test 2"
}
]