Skip to main content

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.

πŸ’‘ Tip

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:

Example request
const document = await strapi.documents("api::article.article").findOne({
fields: ["title", "slug"],
});
Example response
{
"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…

Example request
const document = await strapi.documents("api::article.article").findFirst({
fields: ["title", "slug"],
});
Example response
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article"
}

Selecting fields with findMany() queries​

To select fields to return while finding documents…

Example request
const documents = await strapi.documents("api::article.article").findMany({
fields: ["title", "slug"],
});
Example response
[
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article"
}
// ...
]

Selecting fields with create() queries​

To select fields to return while creating documents…

Example request
const document = await strapi.documents("api::article.article").create({
data: {
title: "Test Article",
slug: "test-article",
body: "Test 1",
headerImage: 2,
},
fields: ["title", "slug"],
});
Example response
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article",
"slug": "test-article"
}

Selecting fields with update() queries​

To select fields to return while updating documents…

Example request
const document = await strapi.documents("api::article.article").update({
id: "cjld2cjxh0000qzrmn831i7rn",
data: {
title: "Test Article Updated",
},
fields: ["title"],
});
Example response
{
"id": "cjld2cjxh0000qzrmn831i7rn",
"title": "Test Article Updated"
}

Selecting fields with delete() queries​

To select fields to return while deleting documents…

Example request
const document = await strapi.documents("api::article.article").delete({
id: "cjld2cjxh0000qzrmn831i7rn",
fields: ["title"],
});
Example response
{
"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…

Example request
const document = await strapi.documents("api::article.article").publish({
id: "cjld2cjxh0000qzrmn831i7rn",
fields: ["title"],
});
Example response
{
"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…

Example request
const document = await strapi.documents("api::article.article").unpublish({
id: "cjld2cjxh0000qzrmn831i7rn",
fields: ["title"],
});
Example response
{
"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…

Example request
const document = await strapi.documents("api::article.article").discardDraft({
id: "cjld2cjxh0000qzrmn831i7rn",
fields: ["title"],
});
Example response
{
"id": "cjld2cjxh0000qzrmn831i7rn",
// All of the discarded draft versions are returned
"versions": [
{
"title": "Test Article"
}
]
}