Skip to main content

Defining indexes

Managing indexes in a database is key when you have a lot of volume on your platform and that the time response is critical.

You can define arbitrary indexes on the collections managed by the datastore with the entry in the model called indexes and defined as:

{
"name": "users",
...
"indexes": [{
collection: 'users',
fields: { 'email': 1 },
opts: { name: 'email_unique', unique: true },
}]
}
tip

We are following the mongodb Node.js driver implementation to apply the indexes1.

indexdescription createindexesoptions

A specific route exists to apply these indexes and you can apply them with:

curl -X POST 'http://localhost:3001/api/admin/users/indexes' \
-H 'Authorization: token' \
-H 'Content-Type: application/json' | jq

Default indexes are created out of the box on some specific fields such as created_at, updated_at or the correlation_field specified to a model. And you can define your own ones!

OpenAPI

In the OpenAPI specification and more specifically on the contract of the find route, you can identify the fields that are associated to an index in order to write performant requests taking advantages of the model indexes definition.

{
"in": "query",
"name": "created_at",
"description": "<code>index</code> ISO 8601 date format",
"schema": {
"anyOf": [
{
"type": "string",
"description": "<code>index</code> ISO 8601 date format",
"example": "2019-01-01T00:00:00.000Z",
"format": "date-time"
},
{
"type": "array",
"items": {
"type": "string",
"description": "<code>index</code> ISO 8601 date format",
"example": "2019-01-01T00:00:00.000Z",
"format": "date-time"
}
},
{
"type": "object"
}
]
},
"required": false
}

Getting further

Please have a look at the MongoDB indexes documentation to check what you can do with that!

performance, automatic deletion, spatial requests, ...