Document Service API: Selecting 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 select
parameter to return only specific fields with the query results.
You can also use the populate
parameter to populate relations, media fields, components, or dynamic zones (see the populate
parameter documentation).
Selecting fields with findOne()
queriesβ
To select fields to return while finding a specific document with the Document Service API:
const document = await strapi.documents("api::article.article").findOne({
fields: ["title", "slug"],
});
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article"
}
Selecting fields with findFirst()
queriesβ
To select fields to return while finding the first document matching the parametersβ¦
const document = await strapi.documents("api::article.article").findFirst({
fields: ["title", "slug"],
});
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article"
}
Selecting fields with findMany()
queriesβ
To select fields to return while finding documentsβ¦
const documents = await strapi.documents("api::article.article").findMany({
fields: ["title", "slug"],
});
[
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article"
}
// ...
]
Selecting fields with create()
queriesβ
To select fields to return while creating documentsβ¦
const document = await strapi.documents("api::article.article").create({
data: {
title: "Test Article",
slug: "test-article",
body: "Test 1",
headerImage: 2,
},
fields: ["title", "slug"],
});
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article"
}
Selecting fields with update()
queriesβ
To select fields to return while updating documentsβ¦
const document = await strapi.documents("api::article.article").update({
id: "cjld2cjxh0000qzrmn831i7rn",
data: {
title: "Test Article Updated",
},
fields: ["title"],
});
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article Updated"
}
Selecting fields with delete()
queriesβ
To select fields to return while deleting documentsβ¦
const document = await strapi.documents("api::article.article").delete({
id: "cjld2cjxh0000qzrmn831i7rn",
fields: ["title"],
});
{
"id": "cjld2cjxh0000qzrmn831i7rn",
// All of the deleted document's versions are returned
"versions": [
{
"title": "Test Article"
}
]
}
Selecting fields with publish()
queriesβ
To select fields to return while publishing documentsβ¦
const document = await strapi.documents("api::article.article").publish({
id: "cjld2cjxh0000qzrmn831i7rn",
fields: ["title"],
});
{
"id": "cjld2cjxh0000qzrmn831i7rn",
// All of the published locale versions are returned
"versions": [
{
"title": "Test Article"
}
]
}
Selecting fields with unpublish()
queriesβ
To select fields to return while unpublishing documentsβ¦
const document = await strapi.documents("api::article.article").unpublish({
id: "cjld2cjxh0000qzrmn831i7rn",
fields: ["title"],
});
{
"id": "cjld2cjxh0000qzrmn831i7rn",
// All of the unpublished locale versions are returned
"versions": [
{
"title": "Test Article"
}
]
}
Selecting fields with discardDraft()
queriesβ
To select fields to return while discarding draft versions of documents with the Document Service APIβ¦
const document = await strapi.documents("api::article.article").discardDraft({
id: "cjld2cjxh0000qzrmn831i7rn",
fields: ["title"],
});
{
"id": "cjld2cjxh0000qzrmn831i7rn",
// All of the discarded draft versions are returned
"versions": [
{
"title": "Test Article"
}
]
}