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:
REST API
To specify select in the REST API, you can use the select
parameter in your query:
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:
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:
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:
REST API: