文档服务 API
¥Document Service API
文档服务 API 建立在查询引擎 API 之上
¥The Document Service API is built on top of the Query Engine API
用于对文档 执行 CRUD(create、retrieve、update 和 delete)操作。
¥and is used to perform CRUD (create, retrieve, update, and delete) operations on documents .
文档服务 API 也支持 counting 文档,如果在内容类型上启用了 起草并发布,则执行 Strapi 特定的操作,例如 publishing、unpublishing 和 丢弃草稿。
¥The Document Service API also supports counting documents and, if Draft & Publish is enabled on the content-type, performing Strapi-specific operations such as publishing, unpublishing, and discarding drafts.
在 Strapi 5 中,文档在 API 级别由其 documentId 唯一标识。
¥In Strapi 5, documents are uniquely identified by their documentId at the API level.
文档服务 API 取代了 Strapi v4 中使用的实体服务 API (查看 Strapi v4 文档)。
¥The Document Service API replaces the Entity Service API used in Strapi v4 (see Strapi v4 documentation).
有关如何从实体服务 API 迁移到文档的附加信息服务 API 可在 迁移参考 中找到。
¥Additional information on how to migrate from the Entity Service API to the Document Service API can be found in the migration reference.
关系也可以通过文档服务 API 连接、断开和设置,就像使用 REST API 一样(有关示例,请参阅 REST API 关系文档)。
¥Relations can also be connected, disconnected, and set through the Document Service API, just like with the REST API (see the REST API relations documentation for examples).
文档对象
¥Document objects
文档方法返回一个文档对象或文档对象列表,这些对象代表一个稳定 documentId 下的内容条目版本。返回的对象通常包括:
¥Document methods return a document object or a list of document objects, which represent a version of a content entry grouped under a stable documentId. Returned objects typically include:
-
documentId:跨语言环境和草稿/已发布版本的条目持久标识符。¥
documentId: Persistent identifier for the entry across locales and draft/published versions. -
id:特定语言环境/版本记录的数据库标识符。¥
id: Database identifier for the specific locale/version record. -
模型字段:内容类型架构中定义的所有字段。除非你选择启用
populate(参见 填充字段)或使用fields(参见 选择字段)限制字段,否则关系、组件和动态区域不会被填充。¥model fields: All fields defined in the content-type schema. Relations, components, and dynamic zones are not populated unless you opt in with
populate(see Populating fields) or limit fields withfields(see Selecting fields). -
元数据:
publishedAt、createdAt、updatedAt以及createdBy/updatedBy(如果可用)。¥metadata:
publishedAt,createdAt,updatedAt, andcreatedBy/updatedBywhen available.
如果内容类型启用了 起草并发布 和 国际化,则文档对象还可以选择性地包含 status 和 locale 属性。
¥Optionally, document objects can also include a status and locale property if Draft & Publish and Internationalization are enabled for the content-type.
方法概述
¥Method overview
以下各节分别介绍特定方法的参数和示例:
¥Each section below documents the parameters and examples for a specific method:
| 方法 | 目的 |
|---|---|
findOne() | 按 documentId 获取文档,可选择指定语言或状态。 |
findFirst() | 返回第一个符合筛选条件的文档。 |
findMany() | 使用筛选器、排序和分页列出文档。 |
create() | 创建文档,可以选择指定语言环境。 |
update() | 使用 documentId 更新文档。 |
delete() | 删除文档或特定语言版本。 |
publish() | 发布文档的草稿版本。 |
unpublish() | 将已发布的文档移回草稿。 |
discardDraft() | 删除草稿数据,仅保留已发布版本。 |
count() | 统计与参数匹配的文档数量。 |
findOne()
查找与传递的 documentId 和参数匹配的文档。
¥Find a document matching the passed documentId and parameters.
语法:findOne(parameters: Params) => Document
¥Syntax: findOne(parameters: Params) => Document
参数
¥Parameters
| 范围 | 描述 | 默认 | 类型 |
|---|---|---|---|
documentId | 文档 ID | ID | |
locale | 要查找的文档的语言环境。 | 默认语言环境 | 字符串或 undefined |
status | 如果为内容类型启用了 起草并发布: 发布状态,可以是:
| 'draft' | 'published' 或 'draft' |
fields | 选择字段 返回 | 所有字段 (默认情况下未填充的字段除外) | 目的 |
populate | 填充 结果带有附加字段。 | null | 目的 |
示例
¥Example
如果仅传递 documentId 而没有任何其他参数,findOne() 将返回默认语言环境中的文档草稿版本:
¥If only a documentId is passed without any other parameters, findOne() returns the draft version of a document in the default locale:
await strapi.documents('api::restaurant.restaurant').findOne({
documentId: 'a1b2c3d4e5f6g7h8i9j0klm'
})
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Biscotte Restaurant",
publishedAt: null, // draft version (default)
locale: "en", // default locale
// …
}
如果找到匹配的文档,findOne() 方法返回该文档,否则返回 null。
¥The findOne() method returns the matching document if found, otherwise returns null.
findFirst()
查找与参数匹配的第一个文档。
¥Find the first document matching the parameters.
语法:findFirst(parameters: Params) => Document
¥Syntax: findFirst(parameters: Params) => Document
参数
¥Parameters
| 范围 | 描述 | 默认 | 类型 |
|---|---|---|---|
locale | 要查找的文档的语言环境。 | 默认语言环境 | 字符串或 undefined |
status | 如果为内容类型启用了 起草并发布: 发布状态,可以是:
| 'draft' | 'published' 或 'draft' |
filters | 使用 过滤器 | null | 目的 |
fields | 选择字段 返回 | 所有字段 (默认情况下未填充的字段除外) | 目的 |
populate | 填充 结果带有附加字段。 | null | 目的 |
示例
¥Examples
通用示例
¥Generic example
默认情况下,findFirst() 在默认语言环境中返回传递的唯一标识符(集合类型 ID 或单一类型 ID)的第一个文档的草稿版本:
¥By default, findFirst() returns the draft version, in the default locale, of the first document for the passed unique identifier (collection type id or single type id):
await strapi.documents('api::restaurant.restaurant').findFirst()
{
documentId: "a1b2c3d4e5f6g7h8i9j0klm",
name: "Restaurant Biscotte",
publishedAt: null,
locale: "en"
// …
}