文档服务 API:选择字段
🌐 Document Service API: Selecting fields
Page summary:在文档服务 API 查询中使用
fields参数以选择要随结果返回的特定字段,从而减少在findOne()、findMany()、create()、update()、delete()、publish()及其他文档操作中的数据传输量。🌐 Use the
fieldsparameter in Document Service API queries to select specific fields to return with your results, reducing data payload acrossfindOne(),findMany(),create(),update(),delete(),publish(), and other document operations.
默认情况下,文档服务 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
strapi.documents("api::restaurant.restaurant").findOne()Select fields with findOne()
Select specific fields to return when finding a document by documentId.
使用 findFirst() 查询选择字段
🌐 Select fields with findFirst() queries
strapi.documents("api::restaurant.restaurant").findFirst()Select fields with findFirst()
Select specific fields to return when finding the first matching document.
使用 findMany() 查询选择字段
🌐 Select fields with findMany() queries
strapi.documents("api::restaurant.restaurant").findMany()Select fields with findMany()
Select specific fields to return when finding multiple documents.
使用 create() 查询选择字段
🌐 Select fields with create() queries
strapi.documents("api::restaurant.restaurant").create()Select fields with create()
Select specific fields to return when creating a new document.
使用 update() 查询选择字段
🌐 Select fields with update() queries
strapi.documents("api::restaurant.restaurant").update()Select fields with update()
Select specific fields to return when updating a document.
使用 delete() 查询选择字段
🌐 Select fields with delete() queries
strapi.documents("api::restaurant.restaurant").delete()Select fields with delete()
Select specific fields to return when deleting a document.
使用 publish() 查询选择字段
🌐 Select fields with publish() queries
strapi.documents("api::restaurant.restaurant").publish()Select fields with publish()
Select specific fields to return when publishing a document.
选择包含 unpublish() 查询的字段
🌐 Select fields with unpublish() queries
strapi.documents("api::restaurant.restaurant").unpublish()Select fields with unpublish()
Select specific fields to return when unpublishing a document.
选择包含 discardDraft() 查询的字段
🌐 Select fields with discardDraft() queries
strapi.documents("api::restaurant.restaurant").discardDraft()Select fields with discardDraft()
Select specific fields to return when discarding a draft document.