Document Service API: 結果のソートとページネーション
Document Service API では、クエリ結果のソートとページネーションができます。
ソート
Document Service API の結果をソートするには、クエリに sort パラメータを含めます。
単一フィールドでソート
1 つのフィールドでソートする例です。
リクエスト例
const documents = await strapi.documents("api::article.article").findMany({
sort: "title:asc",
});
レスポンス例
[
{
"documentId": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1"
// ...
},
{
"documentId": "cjld2cjxh0001qzrm5q1j5q7m",
"title": "Test Article 2",
"slug": "test-article-2",
"body": "Test 2"
// ...
}
// ...
]
複数フィールドでソート
複数フィールドでソートするには、配列で渡します。
リクエスト例
const documents = await strapi.documents("api::article.article").findMany({
sort: [{ title: "asc" }, { slug: "desc" }],
});
レスポンス例
[
{
"documentId": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article",
"body": "Test 1"
// ...
},
{
"documentId": "cjld2cjxh0001qzrm5q1j5q7m",
"title": "Test Article 2",
"slug": "test-article-2",
"body": "Test 2"
// ...
}
// ...
]
ページネーション
ページネーションするには、limit と start を渡します。
リクエスト例
const documents = await strapi.documents("api::article.article").findMany({
limit: 10,
start: 0,
});
レスポンス例
[
{
"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 件)
]