Skip to content

BUCKET.INDEX

Manages indexes on bucket fields. Indexes accelerate queries by allowing efficient lookups on specific fields.

SubcommandDescription
CREATECreate a new index on one or more fields.
LISTList all indexes on a bucket.
DESCRIBEGet detailed information about an index.
DROPDrop an existing index.
TASKSList background maintenance tasks for an index.
ANALYZETrigger index statistics analysis.

Creates one or more indexes on bucket fields.

BUCKET.INDEX CREATE <bucket> <schema>
ParameterTypeRequiredDescription
bucketstringYesName of the target bucket. The bucket must already exist.
schemaJSONYesIndex schema defining the fields to index.

The schema is a JSON object that can contain single-field indexes, compound indexes, or both:

{
"<field>": { "bson_type": "<type>" [, "multi_key": true] [, "name": "<name>"] },
"$compound": [ { "fields": [ { "selector": "<field>", "bson_type": "<type>" }, ... ] [, "name": "<name>"] } ]
}

Field selectors use dot notation to address nested fields inside documents. Arrays are traversed automatically. When a selector crosses an array, each element is evaluated independently.

SelectorTargets
nameTop-level field name.
address.citycity inside the nested object address.
tagsThe tags field itself (use with multi_key for arrays).
orders.totaltotal inside each element of the orders array.

Example document:

{
"username": "alice",
"address": { "city": "Istanbul", "zip": "34000" },
"tags": ["admin", "editor"],
"orders": [
{ "total": 120, "status": "shipped" },
{ "total": 45, "status": "pending" }
]
}
  • Selector address.city reaches "Istanbul".
  • Selector tags with multi_key: true indexes each element ("admin", "editor") separately.
  • Selector orders.total with multi_key: true indexes each order’s total (120, 45) separately.

Each top-level key (other than $compound) is a field selector. The value defines the index properties:

{
"field_name": {
"bson_type": "type",
"multi_key": true|false,
"name": "optional_custom_name"
}
}
PropertyTypeRequiredDescription
bson_typestringYesThe BSON type of the field values. See Supported BSON Types.
multi_keybooleanNoWhen true, creates a multi-key index for array fields. Each array element generates a separate index entry. Default: false.
namestringNoCustom name for the index. If omitted, a name is auto-generated from the selector and type.
collationobjectNoCollation spec for locale-aware string ordering. Only valid for string type. See Collation.

The $compound key holds an array of compound index definitions. Each definition specifies an ordered list of fields:

{
"$compound": [
{
"name": "optional_custom_name",
"fields": [
{ "selector": "field_a", "bson_type": "string" },
{ "selector": "field_b", "bson_type": "int32", "multi_key": false }
]
}
]
}

Each field in the fields array supports:

PropertyTypeRequiredDescription
selectorstringYesDot-notation path to the field.
bson_typestringYesThe BSON type of the field values. See Supported BSON Types.
multi_keybooleanNoWhen true, creates a multi-key index entry per array element. Default: false.

The compound index definition also supports:

PropertyTypeRequiredDescription
namestringNoCustom name for the compound index. If omitted, a name is auto-generated.
collationobjectNoCollation spec for locale-aware string ordering. Requires at least one string field. See Collation.

Constraints:

  • A compound index must have at least two fields.
  • A compound index supports at most 32 fields.
  • At most one field can have multi_key enabled.
  • Each field selector must appear exactly once within a compound index.

See Compound Indexes for detailed rules including the prefix rule and supported operators.

When multi_key is set to true, the index is designed for array fields. Each element in the array creates a separate index entry, allowing queries to match documents where any array element satisfies the condition.

Limitations:

  • Undefined ordering: Result ordering is undefined with multi-key indexes. Since each document can have multiple index entries (one per array element), the order in which documents are returned cannot be guaranteed.
  • Index size: Multi-key indexes can be significantly larger than regular indexes because each array element creates a separate index entry.
  • Type matching: Only array elements matching the specified bson_type are indexed.

Returns OK on success.

Error CodeDescription
ERRThe index already exists.
ERRThe schema is invalid.
ERRUnknown BSON type.
NOSUCHBUCKETThe specified bucket does not exist.
BUCKETBEINGREMOVEDThe target bucket is being removed.

Create a single-field index:

> BUCKET.INDEX CREATE users '{"username": {"bson_type": "string"}}'
OK

Create multiple indexes at once:

> BUCKET.INDEX CREATE users '{"age": {"bson_type": "int32"}, "email": {"bson_type": "string"}}'
OK

Create an index with a custom name:

> BUCKET.INDEX CREATE users '{"username": {"bson_type": "string", "name": "idx_username"}}'
OK

Create a multi-key index for array fields:

> BUCKET.INDEX CREATE products '{"tags": {"bson_type": "string", "multi_key": true}}'
OK

Index a nested field using dot notation:

> BUCKET.INDEX CREATE users '{"address.city": {"bson_type": "string"}}'
OK

Index elements inside an array of objects:

> BUCKET.INDEX CREATE users '{"orders.total": {"bson_type": "int32", "multi_key": true}}'
OK

Create a single-field index with collation:

> BUCKET.INDEX CREATE users '{
"username": {
"bson_type": "string",
"collation": {"locale": "tr", "strength": 2}
}
}'
OK

Create a compound index:

> BUCKET.INDEX CREATE products '{
"$compound": [{
"name": "idx_cat_price",
"fields": [
{"selector": "category", "bson_type": "string"},
{"selector": "price", "bson_type": "double"}
]
}]
}'
OK

Create a compound index with collation:

> BUCKET.INDEX CREATE products '{
"$compound": [{
"fields": [
{"selector": "category", "bson_type": "string"},
{"selector": "price", "bson_type": "double"}
],
"collation": {"locale": "en"}
}]
}'
OK

Create single-field and compound indexes together:

> BUCKET.INDEX CREATE products '{
"email": {"bson_type": "string"},
"$compound": [{
"fields": [
{"selector": "status", "bson_type": "string"},
{"selector": "region", "bson_type": "string"}
]
}]
}'
OK

Lists all indexes defined on a bucket.

BUCKET.INDEX LIST <bucket>
ParameterTypeRequiredDescription
bucketstringYesName of the bucket.

Returns an array of index names.

Error CodeDescription
NOSUCHBUCKETThe specified bucket does not exist.
BUCKETBEINGREMOVEDThe bucket is being removed.
> BUCKET.INDEX LIST users
1) "primary-index"
2) "selector:username.bsonType:STRING"
3) "selector:age.bsonType:INT32"

Gets detailed information about a specific index.

BUCKET.INDEX DESCRIBE <bucket> <index>
ParameterTypeRequiredDescription
bucketstringYesName of the bucket.
indexstringYesName of the index to describe.

Returns a map with the following fields:

FieldTypeDescription
index_typestringKind of the index: single_field, compound, or vector.
idintegerIndex identifier.
selectorstringThe field selector the index is built on.
bson_typestringThe BSON type of indexed values.
statusstringCurrent index status. See Index Lifecycle.
collationmapCollation configuration (see below). All values are null when no collation is set. Only present for single-field and compound indexes.
statisticsmapIndex statistics including cardinality.
FieldTypeDescription
localestringICU locale identifier (e.g., "en", "tr").
strengthintegerComparison strength level (1-5).
case_levelbooleanWhether to include case-level comparisons.
case_firststringSort order of case differences ("upper", "lower", "off").
numeric_orderingbooleanWhether to compare numeric strings as numbers.
alternatestringHandling of variable-weight characters ("non-ignorable", "shifted").
backwardsbooleanWhether to reverse secondary-level comparisons (for French).
normalizationbooleanWhether to perform Unicode normalization.
max_variablestringWhich characters are ignorable when alternate="shifted" ("punct", "space").
Error CodeDescription
NOSUCHBUCKETThe specified bucket does not exist.
NOSUCHINDEXThe specified index does not exist.
BUCKETBEINGREMOVEDThe bucket is being removed.
> BUCKET.INDEX DESCRIBE users "selector:username.bsonType:STRING"
index_type -> "single_field"
id -> 2
selector -> "username"
bson_type -> "STRING"
status -> "WAITING"
collation -> {locale -> (nil), strength -> (nil), case_level -> (nil), case_first -> (nil), numeric_ordering -> (nil), alternate -> (nil), backwards -> (nil), normalization -> (nil), max_variable -> (nil)}
statistics -> {cardinality -> 0}

Drops an existing index from a bucket.

BUCKET.INDEX DROP <bucket> <index>
ParameterTypeRequiredDescription
bucketstringYesName of the bucket.
indexstringYesName of the index to drop.

Returns OK on success. The index is marked as DROPPED and a background task is created to clean up the index data.

Error CodeDescription
ERRCannot drop the primary index (primary-index).
ERRThe index is already in the DROPPED status.
ERRThe index has active tasks.
NOSUCHBUCKETThe specified bucket does not exist.
NOSUCHINDEXThe specified index does not exist.
BUCKETBEINGREMOVEDThe bucket is being removed.
> BUCKET.INDEX DROP users "selector:username.bsonType:STRING"
OK

Attempting to drop the primary index:

> BUCKET.INDEX DROP users "primary-index"
(error) ERR Cannot drop the primary index

Lists background maintenance tasks associated with an index.

BUCKET.INDEX TASKS <bucket> <index>
ParameterTypeRequiredDescription
bucketstringYesName of the bucket.
indexstringYesName of the index.

Returns a map where each key is a task ID and the value contains task details:

For BUILD tasks:

FieldTypeDescription
kindstringTask type (BUILD).
cursorstringCurrent position in the build process.
lowerstringLower bound position.
upperstringUpper bound position.
statusstringTask status.
errorstringError message if failed.

For DROP tasks:

FieldTypeDescription
kindstringTask type (DROP).
statusstringTask status.
errorstringError message if failed.

For BOUNDARY tasks:

FieldTypeDescription
kindstringTask type (BOUNDARY).
statusstringTask status.
errorstringError message if failed.

For ANALYZE tasks:

FieldTypeDescription
kindstringTask type (ANALYZE).
statusstringTask status.
errorstringError message if failed.
Error CodeDescription
NOSUCHBUCKETThe specified bucket does not exist.
BUCKETBEINGREMOVEDThe bucket is being removed.
> BUCKET.INDEX TASKS users "selector:username.bsonType:STRING"
"5K8G4R000000000000000000" -> {
kind -> "BUILD"
cursor -> "5K8G4R000000000000000500"
lower -> "0000000000000000000000000"
upper -> "5K8G4R000000000000001000"
status -> "RUNNING"
error -> ""
}

Trigger index statistics analysis. Statistics help the query optimizer make better decisions.

BUCKET.INDEX ANALYZE <bucket> <index>
ParameterTypeRequiredDescription
bucketstringYesName of the bucket.
indexstringYesName of the index to analyze.

Returns OK on success. A background task is created to compute index statistics.

Error CodeDescription
ERRAn analysis task already exists for this index.
ERRThe index is not in the READY state. Only indexes that have completed building can be analyzed.
NOSUCHBUCKETThe specified bucket does not exist.
NOSUCHINDEXThe specified index does not exist.
BUCKETBEINGREMOVEDThe bucket is being removed.
> BUCKET.INDEX ANALYZE users "selector:username.bsonType:STRING"
OK

Indexes go through the following states:

StatusDescription
WAITINGIndex is created but background building has not started yet.
BUILDINGIndex is being built by a background task.
READYIndex is fully built and available for queries.
DROPPEDIndex is marked for deletion; background cleanup is in progress.
FAILEDIndex building failed due to an error.

The following BSON types can be indexed:

TypeDescription
stringUTF-8 string values.
int3232-bit signed integers.
int6464-bit signed integers.
double64-bit IEEE 754 floating point.
booleanBoolean values (true / false).
datetimeUTC datetime (milliseconds since Unix epoch).
timestampInternal timestamp type.
binaryBinary data.
objectidObjectId values.
decimal128128-bit decimal floating point. Not yet fully supported for indexing.