Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Select

By default, Payload's APIs will return all fields for a given collection or global. But, you may not need all of that data for all of your queries. Sometimes, you might want just a few fields from the response, which can speed up the Payload API and reduce the amount of JSON that is sent to you from the API.

This is where Payload's select feature comes in. Here, you can define exactly which fields you'd like to retrieve from the API.

Local API

To specify select in the Local API, you can use the select option in your query:

1
// Include mode
2
const getPosts = async () => {
3
const posts = await payload.find({
4
collection: 'posts',
5
select: {
6
text: true,
7
// select a specific field from group
8
group: {
9
number: true
10
},
11
// select all fields from array
12
array: true,
13
},
14
})
15
16
return posts
17
}
18
19
// Exclude mode
20
const getPosts = async () => {
21
const posts = await payload.find({
22
collection: 'posts',
23
// Select everything except for array and group.number
24
select: {
25
array: false,
26
group: {
27
number: false
28
}
29
},
30
})
31
32
return posts
33
}

REST API

To specify select in the REST API, you can use the select parameter in your query:

1
fetch('https://localhost:3000/api/posts?select[color]=true&select[group][number]=true')
2
.then((res) => res.json())
3
.then((data) => console.log(data))

To understand the syntax, you need to understand that complex URL search strings are parsed into a JSON object. This one isn't too bad, but more complex queries get unavoidably more difficult to write.

For this reason, we recommend to use the extremely helpful and ubiquitous qs-esm package to parse your JSON / object-formatted queries into query strings:

1
import { stringify } from 'qs-esm'
2
3
const select = {
4
text: true,
5
group: {
6
number: true
7
}
8
// This query could be much more complex
9
// and QS would handle it beautifully
10
}
11
12
const getPosts = async () => {
13
const stringifiedQuery = stringify(
14
{
15
select, // ensure that `qs` adds the `select` property, too!
16
},
17
{ addQueryPrefix: true },
18
)
19
20
const response = await fetch(`http://localhost:3000/api/posts${stringifiedQuery}`)
21
// Continue to handle the response below...
22
}

defaultPopulate collection config property

The defaultPopulate property allows you specify which fields to select when populating the collection from another document. This is especially useful for links where only the slug is needed instead of the entire document.

With this feature, you can dramatically reduce the amount of JSON that is populated from Relationship or Upload fields.

For example, in your content model, you might have a Link field which links out to a different page. When you go to retrieve these links, you really only need the slug of the page.

Loading all of the page content, its related links, and everything else is going to be overkill and will bog down your Payload APIs. Instead, you can define the defaultPopulate property on your Pages collection, so that when Payload "populates" a related Page, it only selects the slug field and therefore returns significantly less JSON:

1
import type { CollectionConfig } from 'payload'
2
3
import { lexicalEditor, LinkFeature } from '@payloadcms/richtext-lexical'
4
import { slateEditor } from '@payloadcms/richtext-slate'
5
6
// The TSlug generic can be passed to have type safety for `defaultPopulate`.
7
// If avoided, the `defaultPopulate` type resolves to `SelectType`.
8
export const Pages: CollectionConfig<'pages'> = {
9
slug: 'pages',
10
// Specify `select`.
11
defaultPopulate: {
12
slug: true,
13
},
14
fields: [
15
{
16
name: 'slug',
17
type: 'text',
18
required: true,
19
},
20
],
21
}

populate

Setting defaultPopulate will enforce that each time Payload performs a "population" of a related document, only the fields specified will be queried and returned. However, you can override defaultPopulate with the populate property in the Local and REST API:

Local API:

1
const getPosts = async () => {
2
const posts = await payload.find({
3
collection: 'posts',
4
populate: {
5
// Select only `text` from populated docs in the "pages" collection
6
// Now, no matter what the `defaultPopulate` is set to on the "pages" collection,
7
// it will be overridden, and the `text` field will be returned instead.
8
pages: {
9
text: true,
10
},
11
},
12
})
13
14
return posts
15
}

REST API:

1
fetch('https://localhost:3000/api/posts?populate[pages][text]=true')
2
.then((res) => res.json())
3
.then((data) => console.log(data))
Next

Pagination