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.

Indexes

Database indexes are a way to optimize the performance of your database by allowing it to quickly locate and retrieve data. If you have a field that you frequently query or sort by, adding an index to that field can significantly improve the speed of those operations.

When your query runs, the database will not scan the entire document to find that one field, but will instead use the index to quickly locate the data.

To index a field, set the index option to true in your field's config:

1
import type { CollectionConfig } from 'payload'
2
3
export MyCollection: CollectionConfig = {
4
// ...
5
fields: [
6
// ...
7
{
8
name: 'title',
9
type: 'text',
10
index: true,
11
},
12
]
13
}

Compound Indexes

In addition to indexing single fields, you can also create compound indexes that index multiple fields together. This can be useful for optimizing queries that filter or sort by multiple fields.

To create a compound index, use the indexes option in your Collection Config:

1
import type { CollectionConfig } from 'payload'
2
3
export const MyCollection: CollectionConfig = {
4
// ...
5
fields: [
6
// ...
7
],
8
indexes: [
9
{
10
fields: ['title', 'createdAt'],
11
unique: true, // Optional, if you want the combination of fields to be unique
12
},
13
],
14
}
Next

MongoDB