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.

💡 提示

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

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

✏️ 注意

虽然建议在 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:

Example request


const document = await strapi.documents("api::restaurant.restaurant").findOne({


documentId: 'a1b2c3d4e5f6g7h8i9j0klm',
fields: ["name", "description"],
});
Example response
{
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:

Example request


const document = await strapi.documents("api::restaurant.restaurant").findFirst({


fields: ["name", "description"],
});
Example response
{
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:

Example request


const documents = await strapi.documents("api::restaurant.restaurant").findMany({


fields: ["name", "description"],
});
Example response
[
{
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:

Example request


const document = await strapi.documents("api::restaurant.restaurant").create({


data: {
name: "Restaurant B",
description: "Description for the restaurant",
},
fields: ["name", "description"],
});
Example response
{
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:

Example request


const document = await strapi.documents("api::restaurant.restaurant").update({


documentId: "fmtr6d7ktzpgrijqaqgr6vxs",
data: {
name: "Restaurant C",
},
fields: ["name"],
});
Example response
{ 
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:

Example request


const document = await strapi.documents("api::restaurant.restaurant").delete({


documentId: "fmtr6d7ktzpgrijqaqgr6vxs",
fields: ["name"],
});
Example response
  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:

Example request


const document = await strapi.documents("api::restaurant.restaurant").publish({


documentId: "fmtr6d7ktzpgrijqaqgr6vxs",
fields: ["name"],
});
Example response
{
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:

Example request


const document = await strapi.documents("api::restaurant.restaurant").unpublish({


documentId: "cjld2cjxh0000qzrmn831i7rn",
fields: ["name"],
});
Example response
{
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:

Example request


const document = await strapi.documents("api::restaurant.restaurant").discardDraft({


documentId: "fmtr6d7ktzpgrijqaqgr6vxs",
fields: ["name"],
});
Example response
{
documentId: "fmtr6d7ktzpgrijqaqgr6vxs",
// All of the discarded draft entries are returned
entries: [
{
"name": "Restaurant B"
}
]
}