Skip to main content

クエリエンジン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.

クエリエンジンAPIは、結果を並べ替えおよびページネーションする機能を提供します。

並べ替え

クエリエンジンで返される結果を並べ替えるには、orderByパラメーターを使用します。結果は、単一または複数の属性に基づいて並べ替えることができ、関連性のある並べ替えも使用できます。

単一

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

// 単一の並べ替え方向
AI Marketer.db.query('api::article.article').findMany({
orderBy: { id: 'asc' },
});

複数

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

// 複数の並べ替え方向
AI Marketer.db.query('api::article.article').findMany({
orderBy: [{ title: 'asc' }, { publishedAt: 'desc' }],
});

関連性のある並べ替え

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

ページネーション

クエリエンジンAPIで返される結果をページネーションするには、offsetlimitパラメーターを使用します:

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