Skip to main content

文档服务 API:对结果进行排序和分页

¥Document Service API: Sorting and paginating results

文档服务 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

要根据单个字段对结果进行排序:

¥To sort results based on a single field:

Example request


const documents = await strapi.documents("api::article.article").findMany({


sort: "title:asc",
});
Example response
[
{
"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

要对多个字段进行排序,请将它们全部传递到一个数组中:

¥To sort on multiple fields, pass them all in an array:

Example request


const documents = await strapi.documents("api::article.article").findMany({


sort: [{ title: "asc" }, { slug: "desc" }],
});
Example response
[
{
"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

要对结果进行分页,请传递 limitstart 参数:

¥To paginate results, pass the limit and start parameters:

Example request


const documents = await strapi.documents("api::article.article").findMany({


limit: 10,
start: 0,
});
Example response
[
{
"documentId": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1"
// ...
},
{
"documentId": "cjld2cjxh0001qzrm5q1j5q7m",
"title": "Test Article 2",
"slug": "test-article-2",
"body": "Test 2"
// ...
}
// ... (8 more)
]