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 const 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
}

Indexing documents only when fields exist

With MongoDB, use requireExists to limit a compound index to documents where every listed field is present:

1
indexes: [
2
{
3
fields: ['tenant', 'slug'],
4
requireExists: ['slug'],
5
unique: true,
6
},
7
]

This allows multiple documents in the same tenant to omit slug, while still rejecting duplicate combinations of tenant and a present slug. MongoDB implements the condition with a partialFilterExpression using $exists: true for each path.

  • Paths use the same dot notation as fields, such as metadata.code. A filter path can also refer to a field outside the index keys. Every path must identify a configured field.
  • Unique indexes with requireExists cannot use index keys nested inside arrays or blocks, because uniqueness validation does not support matching compound keys across their rows. Existence filter paths may still traverse arrays.
  • An explicit null counts as present. Documents with slug: null participate in the unique index. Only missing fields are excluded.
  • When an index key or existence filter is localized, Payload creates a separate partial index for each locale. A document can participate in one locale's index without supplying the field in every locale.
  • The option also works on non-unique compound indexes. Version indexes retain the filter and remain non-unique.
  • requireExists is MongoDB only. SQL adapters ignore it and retain their existing index and NULL behavior.

For an existing collection, update its database indexes through your normal index-management or migration workflow. Adding the option to the config alone does not change an existing MongoDB index when automatic index management is disabled.

Unique Fields

Setting unique: true on a field creates a collection-wide database unique index on that field's path. This means no two documents in the collection can share the same value for that field.

Localized fields and MongoDB indexes

When you set index: true or unique: true on a localized field, MongoDB creates one index per locale path (e.g., slug.en, slug.da-dk, etc.). With many locales and indexed fields, this can quickly approach MongoDB's per-collection index limit.

If you know you'll query specifically by a locale, you can insert a custom MongoDB index for the locale path manually or with a migration script.

Was this page helpful?

Next

MongoDB