Skip to main content

Ordering and Paginating with the Query Engine API

Caution

In most cases you should not use the Query Engine API and rather use the Document Service API.

Only use the Query Engine API if you exactly know what you are doing, for instance if you want to use a lower-level API that directly interacts with unique rows of the database.

Please keep in mind that the Query Engine API is not aware of the most advanced Strapi 5 features like Draft & Publish, Internationalization, Content History, and possibly more.

The Query Engine API offers the ability to order and paginate results.

Ordering

To order results returned by the Query Engine, use the orderBy parameter. Results can be ordered based on a single or on multiple attributes and can also use relational ordering.

Single

strapi.db.query('api::article.article').findMany({
orderBy: 'id',
});

// single with direction
strapi.db.query('api::article.article').findMany({
orderBy: { id: 'asc' },
});

Multiple

strapi.db.query('api::article.article').findMany({
orderBy: ['id', 'name'],
});

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

Relational ordering

strapi.db.query('api::article.article').findMany({
orderBy: {
author: {
name: 'asc',
},
},
});

Pagination

To paginate results returned by the Query Engine API, use the offset and limit parameters:

strapi.db.query('api::article.article').findMany({
offset: 15,
limit: 10,
});