跳到主要内容

Ordering and Paginating with the Entity Service API

Caution

The Entity Service API is deprecated in Strapi v5. Please consider using the Document Service API instead.

The Entity Service API offers the ability to order and paginate results found with its findMany() method.

Ordering

To order results returned by the Entity Service API, use the sort parameter. Results can be ordered based on a single or on multiple attribute(s) and can also use relational ordering.

Single

To order results by a single field, pass it to the sort parameter either:

  • as a string to sort with the default ascending order, or
  • as an object to define both the field name and the order (i.e. 'asc' for ascending order or 'desc' for descending order)
strapi.entityService.findMany('api::article.article', {
sort: 'id',
});

// single with direction
strapi.entityService.findMany('api::article.article', {
sort: { id: 'desc' },
});

Multiple

To order results by multiple fields, pass the fields as an array to the sort parameter either:

  • as an array of strings to sort multiple fields using the default ascending order, or
  • as an array of objects to define both the field name and the order (i.e. 'asc' for ascending order or 'desc' for descending order)
strapi.entityService.findMany('api::article.article', {
sort: ['publishDate', 'name'],
});

// multiple with direction
strapi.entityService.findMany('api::article.article', {
sort: [{ title: 'asc' }, { publishedAt: 'desc' }],
});

Relational ordering

Fields can also be sorted based on fields from relations:

strapi.entityService.findMany('api::article.article', {
sort: {
author: {
name: 'asc',
},
},
});

Pagination

To paginate results returned by the Entity Service API, you can use the start and limit parameters:

strapi.entityService.findMany('api::article.article', {
start: 10,
limit: 15,
});

You may instead use the page and pageSize parameters:

strapi.entityService.findMany('api::article.article', {
page: 1,
pageSize: 15,
});