REST API: フィルター
REST API では、「ドキュメント一覧を取得」 の結果にフィルターをかけられます。
オプション機能を使うと、次のようなフィルターも利用できます。
- Internationalization (i18n) プラグイン がコンテンツタイプで有効な場合、ロケールで絞り込めます。
- 下書きと公開 が有効な場合、既定の
publishedまたはdraftの状態で絞り込めます。
Strapi takes advantage of the ability of the `qs` library to parse nested objects to create more complex queries.
Use qs directly to generate complex queries instead of creating them manually. Examples in this documentation showcase how you can use qs.
You can also use the interactive query builder if you prefer playing with our online tool instead of generating queries with qs on your machine.
クエリでは次の構文で filters パラメータを渡せます。
GET /api/:pluralApiId?filters[field][operator]=value
利用できるオペレータは次のとおりです。
| オペレータ | 説明 |
|---|---|
$eq | 等しい |
$eqi | 等しい(大文字小文字を区別しない) |
$ne | 等しくない |
$nei | 等しくない(大文字小文字を区別しない) |
$lt | 未満 |
$lte | 以下 |
$gt | より大きい |
$gte | 以上 |
$in | 配列に含まれる |
$notIn | 配列に含まれない |
$contains | 含む |
$notContains | 含まない |
$containsi | 含む(大文字小文字を区別しない) |
$notContainsi | 含まない(大文字小文字を区別しない) |
$null | null である |
$notNull | null でない |
$between | 範囲内である |
$startsWith | 前方一致 |
$startsWithi | 前方一致(大文字小文字を区別しない) |
$endsWith | 後方一致 |
$endsWithi | 後方一致(大文字小文字を区別しない) |
$or | フィルターを OR で結合 |
$and | フィルターを AND で結合 |
$not | フィルターを NOT で結合 |
filters オブジェクトに複数フィールドを渡すと、暗黙的に $and で結合されます(例: GET /api/restaurants?filters[stars][$gte]=3&filters[open][$eq]=true は、営業中かつ星 3 以上のレストランのみ返します)。
$and、$or、$not は相互にネストできます。
既定では、フィルターは Content-Type Builder と CLI が生成する find エンドポイントからのみ使えます。
例: 名(ファーストネーム)が「John」のユーザーを検索
完全一致には $eq を使います。
GET /api/users?filters[username][$eq]=John
JavaScript のクエリ(qs ライブラリで組み立て):
The query URL above was built using the `qs` library.
qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.
const qs = require('qs');
const query = qs.stringify({
filters: {
username: {
$eq: 'John',
},
},
}, {
encodeValuesOnly: true, // prettify URL
});
await request(`/api/users?${query}`);
{
"data": [
{
"id": 1,
"documentId": "znrlzntu9ei5onjvwfaalu2v",
"username": "John",
"email": "john@test.com",
"provider": "local",
"confirmed": true,
"blocked": false,
"createdAt": "2021-12-03T20:08:17.740Z",
"updatedAt": "2021-12-03T20:08:17.740Z"
}
],
"meta": {
"pagination": {
"page": 1,
"pageSize": 25,
"pageCount": 1,
"total": 1
}
}
例: id が 3、6、8 のレストランを複数取得
配列の 値と $in で、複数の完全一致を検索できます。
GET /api/restaurants?filters[id][$in][0]=3&filters[id][$in][1]=6&filters[id][$in][2]=8
JavaScript のクエリ(qs ライブラリで組み立て):
The query URL above was built using the `qs` library.
qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.
const qs = require('qs');
const query = qs.stringify({
filters: {
id: {
$in: [3, 6, 8],
},
},
}, {
encodeValuesOnly: true, // prettify URL
});
await request(`/api/restaurants?${query}`);
{
"data": [
{
"id": 3,
"documentId": "ethwxjxtvuxl89jq720e38uk",
"name": "test3",
// ...
},
{
"id": 6,
"documentId": "ethwxjxtvuxl89jq720e38uk",
"name": "test6",
// ...
},
{
"id": 8,
"documentId": "cf07g1dbusqr8mzmlbqvlegx",
"name": "test8",
// ...
},
],
"meta": {
// ...
}
}