Document Service API: フィールドの選択
既定では、Document Service API はドキュメントのすべてのフィールドを返しますが、どのフィールドもポピュレートしません。このページでは、fields パラメータでクエリ結果に含めるフィールドだけを指定する方法を説明します。
リレーション、メディア、コンポーネント、ダイナミックゾーンをポピュレートするには populate パラメータも使えます(populate パラメータ のドキュメントを参照)。
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'
}
]
}