文档服务 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).
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.