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 fields
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).
Select 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"],
});
{
documentId: "cjld2cjxh0000qzrmn831i7rn",
title: "Test Article",
slug: "test-article"
}
Select fields with findFirst()
queries
To select fields to return while finding the first document matching the parameters with the Document Service API:
const document = await strapi.documents("api::article.article").findFirst({
fields: ["title", "slug"],
});
{
documentId: "cjld2cjxh0000qzrmn831i7rn",
title: "Test Article",
slug: "test-article" …
}
Select fields with findMany()
queries
To select fields to return while finding documents with the Document Service API:
const documents = await strapi.documents("api::article.article").findMany({
fields: ["title", "slug"],
});
[
{
documentId: "cjld2cjxh0000qzrmn831i7rn",
title: "Test Article",
slug: "test-article"
}
// ...
]
Select fields with create()
queries
To select fields to return while creating documents with the Document Service API:
const document = await strapi.documents("api::article.article").create({
data: {
title: "Test Article",
slug: "test-article",
body: "Test 1",
headerImage: 2,
},
fields: ["title", "slug"],
});
{
documentId: "cjld2cjxh0000qzrmn831i7rn",
title: "Test Article",
slug: "test-article"
}
Select fields with update()
queries
To select fields to return while updating documents with the Document Service API:
const document = await strapi.documents("api::article.article").update({
documentId: "cjld2cjxh0000qzrmn831i7rn",
data: {
title: "Test Article Updated",
},
fields: ["title"],
});
{
documentId: "cjld2cjxh0000qzrmn831i7rn",
title: "Test Article Updated"
}
Select fields with delete()
queries
To select fields to return while deleting documents with the Document Service API:
const document = await strapi.documents("api::article.article").delete({
documentId: "cjld2cjxh0000qzrmn831i7rn",
fields: ["title"],
});
{
documentId: "cjld2cjxh0000qzrmn831i7rn",
// All of the deleted document's versions are returned
"versions": [
{
"title": "Test Article"
}
]
}
Select fields with publish()
queries
To select fields to return while publishing documents with the Document Service API:
const document = await strapi.documents("api::article.article").publish({
documentId: "cjld2cjxh0000qzrmn831i7rn",
fields: ["title"],
});
{
documentId: "cjld2cjxh0000qzrmn831i7rn",
// All of the published locale versions are returned
"versions": [
{
"title": "Test Article"
}
]
}
Select fields with unpublish()
queries
To select fields to return while unpublishing documents with the Document Service API:
const document = await strapi.documents("api::article.article").unpublish({
documentId: "cjld2cjxh0000qzrmn831i7rn",
fields: ["title"],
});
{
documentId: "cjld2cjxh0000qzrmn831i7rn",
// All of the unpublished locale versions are returned
"versions": [
{
"title": "Test Article"
}
]
}
Select 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({
documentId: "cjld2cjxh0000qzrmn831i7rn",
fields: ["title"],
});
{
documentId: "cjld2cjxh0000qzrmn831i7rn",
// All of the discarded draft versions are returned
"versions": [
{
"title": "Test Article"
}
]
}