Skip to main content

文档服务 API:选择字段

🌐 Document Service API: Selecting fields

默认情况下,文档服务 API 会返回文档的所有字段,但不会填充任何字段。本页介绍如何使用 fields 参数仅返回查询结果中的特定字段。

🌐 By default the Document Service API returns all the fields of a document but does not populate any fields. This page describes how to use the fields parameter to return only specific fields with the query results.

Tip

你也可以使用 populate 参数来填充关系、媒体字段、组件或动态区域(参见 populate 参数 文档)。

🌐 You can also use the populate parameter to populate relations, media fields, components, or dynamic zones (see the populate parameter documentation).

Note

虽然建议在 Strapi 5 中使用条目的 documentId 进行定位,但条目仍可能有一个 id 字段,并且你将在返回的响应中看到它。这应该会减轻你从 Strapi 4 过渡的难度。有关更多详细信息,请参考重大变更条目

🌐 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() 查询选择字段

🌐 Select fields with findOne() queries

在使用文档服务 API查找特定文档时选择要返回的字段:

🌐 To select fields to return while finding a specific document with the 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() 查询选择字段

🌐 Select fields with findFirst() queries

在使用文档服务 API 查找第一个匹配参数的文档 时选择要返回的字段:

🌐 To select fields to return while finding the first document matching the parameters with the 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() 查询选择字段

🌐 Select fields with findMany() queries

在使用文档服务 API 查找文档 时选择要返回的字段:

🌐 To select fields to return while finding documents with the 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() 查询选择字段

🌐 Select fields with create() queries

在使用文档服务 API 创建文档 时选择要返回的字段:

🌐 To select fields to return while creating documents with the 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() 查询选择字段

🌐 Select fields with update() queries

在使用文档服务 API 更新文档 时选择要返回的字段:

🌐 To select fields to return while updating documents with the 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() 查询选择字段

🌐 Select fields with delete() queries

在使用文档服务 API 删除文档 时选择要返回的字段:

🌐 To select fields to return while deleting documents with the Document Service API:

示例请求
const document = await strapi.documents("api::restaurant.restaurant").delete({
documentId: "fmtr6d7ktzpgrijqaqgr6vxs",
fields: ["name"],
});
示例响应
  documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
// All of the deleted document's versions are returned
entries: [
{
id: 4,
documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
name: 'Restaurant C',
// …
}
]
}

使用 publish() 查询选择字段

🌐 Select fields with publish() queries

在使用文档服务 API 发布文档 时选择要返回的字段:

🌐 To select fields to return while publishing documents with the Document Service API:

示例请求
const document = await strapi.documents("api::restaurant.restaurant").publish({
documentId: "fmtr6d7ktzpgrijqaqgr6vxs",
fields: ["name"],
});
示例响应
{
documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
// All of the published locale entries are returned
entries: [
{
documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
name: 'Restaurant B'
}
]
}

选择包含 unpublish() 查询的字段

🌐 Select fields with unpublish() queries

在使用文档服务 API取消发布文档时选择要返回的字段:

🌐 To select fields to return while unpublishing documents with the Document Service API:

示例请求
const document = await strapi.documents("api::restaurant.restaurant").unpublish({
documentId: "cjld2cjxh0000qzrmn831i7rn",
fields: ["name"],
});
示例响应
{
documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
// All of the published locale entries are returned
entries: [
{
documentId: 'fmtr6d7ktzpgrijqaqgr6vxs',
name: 'Restaurant B'
}
]
}

选择包含 discardDraft() 查询的字段

🌐 Select fields with discardDraft() queries

使用文档服务 API 时,丢弃文档的草稿版本 并选择返回的字段:

🌐 To select fields to return while discarding draft versions of documents with the Document Service API:

示例请求
const document = await strapi.documents("api::restaurant.restaurant").discardDraft({
documentId: "fmtr6d7ktzpgrijqaqgr6vxs",
fields: ["name"],
});
示例响应
{
documentId: "fmtr6d7ktzpgrijqaqgr6vxs",
// All of the discarded draft entries are returned
entries: [
{
"name": "Restaurant B"
}
]
}