メインコンテンツまでスキップ

Document Service API: フィールドの選択

既定では、Document Service API はドキュメントのすべてのフィールドを返しますが、どのフィールドもポピュレートしません。このページでは、fields パラメータでクエリ結果に含めるフィールドだけを指定する方法を説明します。

Tip

リレーション、メディア、コンポーネント、ダイナミックゾーンをポピュレートするには populate パラメータも使えます(populate パラメータ のドキュメントを参照)。

Note

Though it's recommended to target entries by their documentId in Strapi 5, entries might still have an id field, and you will see it in the returned response. This should ease your transition from Strapi 4. Please refer to the breaking change entry for more details.

findOne() でフィールドを選ぶ

Document Service API で 特定のドキュメントを取得 するときに、返すフィールドを絞る例です。

リクエスト例
const document = await strapi.documents("api::restaurant.restaurant").findOne({
documentId: 'a1b2c3d4e5f6g7h8i9j0klm',
fields: ["name", "description"],
});
レスポンス例
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
description: "Welcome to Biscotte restaurant! …"
}

findFirst() でフィールドを選ぶ

Document Service API で 最初に一致するドキュメントを取得 するときに、返すフィールドを絞る例です。

リクエスト例
const document = await strapi.documents("api::restaurant.restaurant").findFirst({
fields: ["name", "description"],
});
レスポンス例
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
description: "Welcome to Biscotte restaurant! …"
}

findMany() でフィールドを選ぶ

Document Service API で ドキュメント一覧を取得 するときに、返すフィールドを絞る例です。

リクエスト例
const documents = await strapi.documents("api::restaurant.restaurant").findMany({
fields: ["name", "description"],
});
レスポンス例
[
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
description: "Welcome to Biscotte restaurant! …"
}
// ...
]

create() でフィールドを選ぶ

Document Service API で ドキュメントを作成 するときに、レスポンスに含めるフィールドを絞る例です。

リクエスト例
const document = await strapi.documents("api::restaurant.restaurant").create({
data: {
name: "Restaurant B",
description: "Description for the restaurant",
},
fields: ["name", "description"],
});
レスポンス例
{
id: 4,
documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
name: 'Restaurant B',
description: 'Description for the restaurant'
}

update() でフィールドを選ぶ

Document Service API で ドキュメントを更新 するときに、レスポンスに含めるフィールドを絞る例です。

リクエスト例
const document = await strapi.documents("api::restaurant.restaurant").update({
documentId: "fmtr6d7ktzpgrijqaqgr6vxs",
data: {
name: "Restaurant C",
},
fields: ["name"],
});
レスポンス例
{ 
documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
name: 'Restaurant C'
}

delete() でフィールドを選ぶ

Document Service API で ドキュメントを削除 するときに、返却に含めるフィールドを絞る例です。

リクエスト例
const document = await strapi.documents("api::restaurant.restaurant").delete({
documentId: "fmtr6d7ktzpgrijqaqgr6vxs",
fields: ["name"],
});
レスポンス例
{
documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
// 削除されたドキュメントのすべてのバージョンが返る
entries: [
{
id: 4,
documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
name: 'Restaurant C',
// …
}
]
}

publish() でフィールドを選ぶ

Document Service API で ドキュメントを公開 するときに、返却に含めるフィールドを絞る例です。

リクエスト例
const document = await strapi.documents("api::restaurant.restaurant").publish({
documentId: "fmtr6d7ktzpgrijqaqgr6vxs",
fields: ["name"],
});
レスポンス例
{
documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
// 公開されたロケールのエントリがすべて返る
entries: [
{
documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
name: 'Restaurant B'
}
]
}

unpublish() でフィールドを選ぶ

Document Service API で 公開を取り下げる ときに、返却に含めるフィールドを絞る例です。

リクエスト例
const document = await strapi.documents("api::restaurant.restaurant").unpublish({
documentId: "cjld2cjxh0000qzrmn831i7rn",
fields: ["name"],
});
レスポンス例
{
documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
// 取り下げたロケールのエントリが返る
entries: [
{
documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
name: 'Restaurant B'
}
]
}

discardDraft() でフィールドを選ぶ

Document Service API で 下書きを破棄 するときに、返却に含めるフィールドを絞る例です。

リクエスト例
const document = await strapi.documents("api::restaurant.restaurant").discardDraft({
documentId: "fmtr6d7ktzpgrijqaqgr6vxs",
fields: ["name"],
});
レスポンス例
{
"documentId": "fmtr6d7ktzpgrijqaqgr6vxs",
// 破棄した下書きエントリがすべて返る
"entries": [
{
"name": "Restaurant B"
}
]
}