Skip to main content

Document Service API: Sorting and paginating results

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

Sort

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

Sort on a single field

strapi.documents().findMany()

Sort results based on a single field using a string value.

GETstrapi.documents().findMany()
const documents = await strapi.documents("api::article.article").findMany({
sort: "title:asc",
});
Returns
[
{
  "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()

Sort results on multiple fields by passing an array of sort objects.

GETstrapi.documents().findMany()
const documents = await strapi.documents("api::article.article").findMany({
sort: [{ title: "asc" }, { slug: "desc" }],
});
Returns
[
{
  "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()

Paginate results using the limit and start parameters.

GETstrapi.documents().findMany()
const documents = await strapi.documents("api::article.article").findMany({
limit: 10,
start: 0,
});
Returns
[
{
  "documentId": "cjld2cjxh0000qzrmn831i7rn",
  "title": "Test Article",
  "slug": "test-article",
  "body": "Test 1"
},
{
  "documentId": "cjld2cjxh0001qzrm5q1j5q7m",
  "title": "Test Article 2",
  "slug": "test-article-2",
  "body": "Test 2"
}
]
Was this page helpful?