Indexes
POST
/_api/database/:db/index/:collection
Create a new index for a collection.
Request Body Options
| Field | Type | Description | Required |
|---|---|---|---|
| name | string | The name of the index. | Yes |
| field | string | The document field to index (e.g., "email"). | Yes |
| type | string |
Index type. Options: persistent (default), hash, fulltext.
|
No |
| unique | boolean | If true, enforces uniqueness on the indexed field. Default: false. |
No |
{"type": "persistent", "field": "email", "unique": true}
Response
200 OK
application/json
{
"name": "idx_email",
"field": "email",
"type": "persistent",
"unique": true,
"status": "created"
}
GET
/_api/database/:db/index/:collection
List all indexes for a collection.
Response
200 OK
application/json
{
"indexes": [
{ "name": "primary", "type": "primary", "fields": ["_key"], "...": "..." },
{ "name": "idx_email", "type": "persistent", "...": "..." }
]
}
PUT
/_api/database/:db/index/:collection/rebuild
Rebuild all indexes for a collection.
Response
200 OK
application/json
{
"database": "my_db",
"collection": "users",
"documents_indexed": 1250,
"status": "rebuilt"
}
DELETE
/_api/database/:db/index/:collection/:name
Drop an index.
Response
204 No Content
POST
/_api/database/:db/geo/:collection
Create a geo-spatial index.
Request Body Options
| Field | Type | Description | Required |
|---|---|---|---|
| name | string | Index name. | Yes |
| field | string | Field to index (must contain [lat, lon] array or object). | Yes |
Response
200 OK
application/json
{
"name": "geo_location_idx",
"field": "location",
"type": "geo",
"status": "created"
}
POST
/_api/database/:db/geo/:collection/:field/near
Find documents near a coordinate.
Request Body Options
| Field | Type | Description | Required |
|---|---|---|---|
| lat | float | Latitude. | Yes |
| lon | float | Longitude. | Yes |
| limit | integer | Max results. Default: 10. | No |
Response
200 OK
application/json
{
"results": [
{ "document": { "_key": "...", "location": [34.05, -118.25] }, "distance": 123.45 },
{ "document": { "_key": "...", "location": [34.06, -118.26] }, "distance": 234.56 }
],
"count": 2
}
POST
/_api/database/:db/geo/:collection/:field/within
Find documents within a radius.
Request Body Options
| Field | Type | Description | Required |
|---|---|---|---|
| lat | float | Latitude. | Yes |
| lon | float | Longitude. | Yes |
| radius | float | Radius in meters (approx). | Yes |
Response
200 OK
application/json
{
"results": [
{ "_key": "...", "location": [34.05, -118.25] },
{ "_key": "...", "location": [34.06, -118.26] }
],
"count": 2
}
GET
/_api/database/:db/geo/:collection
List all geo indexes on a collection.
Response
{ "indexes": [{ "name": "location_idx", "field": "location" }] }
DELETE
/_api/database/:db/geo/:collection/:name
Delete a geo index by name.
TTL Indexes (Time-To-Live)
Automatically delete documents after a specified time period. Useful for session data, caches, and temporary records.
POST
/_api/database/:db/ttl/:collection
Create a TTL index on a timestamp field.
Request Body Options
| Field | Type | Description | Required |
|---|---|---|---|
| name | string | Index name. | Yes |
| field | string | Field containing Unix timestamp (seconds since epoch). | Yes |
| expire_after_seconds | integer | Seconds after the timestamp value when documents expire. Use 0 to expire at the exact timestamp. | Yes |
{"name": "session_expiry", "field": "expires_at", "expire_after_seconds": 3600}
Response
200 OK
application/json
{
"name": "session_expiry",
"field": "expires_at",
"expire_after_seconds": 3600,
"type": "ttl",
"status": "created"
}
GET
/_api/database/:db/ttl/:collection
List all TTL indexes on a collection.
Response
200 OK
application/json
{
"indexes": [
{"name": "session_expiry", "field": "expires_at", "expire_after_seconds": 3600},
{"name": "cache_ttl", "field": "created_at", "expire_after_seconds": 86400}
]
}
DELETE
/_api/database/:db/ttl/:collection/:name
Drop a TTL index. Documents will no longer be automatically deleted.
Response
204 No Content
How TTL Indexes Work
- The timestamp field must contain a Unix timestamp (seconds since epoch)
- Documents are deleted when:
now >= timestamp + expire_after_seconds - A background worker checks for expired documents every 60 seconds
- Documents without the timestamp field are not deleted
- Use
expire_after_seconds: 0to delete at the exact timestamp