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.
With the Select API, you can define exactly which fields you'd like to retrieve. This can impact the performance of your queries by affecting the load on the database and the size of the response.
Local API
To specify select in the Local API, you can use the select option in your query:
Empty select
The id field is always included in the result, regardless of your select query. If you pass an empty select object, only the id will be returned:
Entity-level select config
Both Collections and Globals accept a top-level select function that runs before every read and lets you transform the caller's select based on request context. It receives { operation, req, select } and returns the final select to apply, replacing the caller's. Return undefined to leave the caller's select untouched.
Common uses:
- Force a field to always be populated so it's available in hooks / access control.
- Differentiate API requests from admin panel requests to optimize payload size.
The function form replaces the caller's select rather than deep-merging, so spread the caller's select into your returned object when you want to add to it instead of replacing it. Per-document data is not available — this runs before the read.
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:
GraphQL API
In the GraphQL API, select is a boolean argument on collection and global queries (including version queries). When set to true, Payload builds a Select projection from your GraphQL selection set and only loads those fields from the database.
Unlike the Local and REST APIs, you do not pass a field map. The fields you request in the GraphQL query become the select projection:
The same argument works on singular document queries and globals:
Without select: true, GraphQL still returns only the fields in your selection set over the wire, but Payload may load the full document from the database. Passing select: true applies the Select API at the database level for better performance.
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:
Was this page helpful?