Skip to content

BUCKET.QUERY

Queries documents from a bucket using a filter expression.

BUCKET.QUERY <bucket> <query> [SORTBY <field> <ASC|DESC>] [RESULTSORT <field> <ASC|DESC>] [PROJECTION <spec>] [LIMIT <n>] [COLLATION <json-spec>]
ParameterTypeRequiredDescription
bucketstringYesName of the bucket to query.
queryJSON or BSONYesFilter expression to match documents. Use {} to match all documents.
SORTBYstring + directionNoSort results by a field. Requires field name followed by ASC or DESC.
RESULTSORTstring + directionNoSort each result batch in memory by any field (indexed or not). Requires field name followed by ASC or DESC. Does not guarantee global ordering across BUCKET.ADVANCE calls. See RESULTSORT.
PROJECTIONJSON or BSONNoProjection specification that controls which fields appear in returned documents. Use {"field": 1} for inclusion or {"field": 0} for exclusion. See Projection.
LIMITintegerNoMaximum number of documents to return per batch. Must be non-negative. When not specified, the session’s default limit is used (default: 100, configurable via SESSION.ATTRIBUTE SET limit <n>).
COLLATIONJSONNoQuery-level collation spec for locale-aware string comparison. Overrides index collation for this query.

The command returns a cursor ID and matching documents. The format depends on the protocol version.

Each returned document includes an _id field (ObjectId) that serves as the document’s primary key.

The encoding format of returned documents depends on the session’s reply_type setting:

FormatResponse TypeDescription
bsonBinaryBSON-encoded document (default)
jsonStringJSON-encoded document

To change the format:

SESSION.ATTRIBUTE SET reply_type bson
SESSION.ATTRIBUTE SET reply_type json

RESP3 (map format):

The response is a map with two keys: cursor_id (integer) and entries (array of documents).

1# "cursor_id" => (integer) <cursor-id>
2# "entries" => [<document-1>, <document-2>, ...]

RESP2 (array format):

The response is an array with two elements: the cursor ID and a nested array of documents.

1) (integer) <cursor-id>
2) 1) <document-1>
2) <document-2>
...

Cursor ID:

The cursor ID is used to fetch more results with BUCKET.ADVANCE. Each query creates a new cursor that stores the query context in the session. The cursor tracks the position in the result set for pagination.

Results are returned in batches. Use the cursor ID with BUCKET.ADVANCE to get more results:

BUCKET.ADVANCE QUERY <cursor-id>

When there are no more results, the command returns an empty result set.

The cursor maintains its state across calls:

  • Query context (filter, sort, limit)
  • Current position in the result set
  • Transaction context (if within an explicit transaction)

BUCKET.QUERY honors the session’s SNAPSHOTREAD setting. When SNAPSHOTREAD ON is active, index scans use snapshot isolation, so they will not cause transactions to conflict with concurrent writes. See SNAPSHOTREAD for details.

BUCKET.QUERY can be executed from any node. When the query is sent to a node that does not own the bucket’s shards, it still returns correct results, but with higher latency because the data is read from the owning nodes. For best performance, use BUCKET.LOCATE to find the node that owns the bucket’s shards and send the query there.

Error CodeDescription
NOSUCHBUCKETThe bucket does not exist.
BUCKETBEINGREMOVEDThe bucket is being removed.
NOSUCHNAMESPACEThe namespace does not exist.
NAMESPACEBEINGREMOVEDThe namespace is being removed.

The following examples assume reply_type is set to json.

Query all documents:

BUCKET.QUERY users '{}'

Response (RESP3):

1# "cursor_id" => (integer) 1
2# "entries" =>
1) {"_id": "6a240c7b5da17d872dc0e102", "name": "Bob", "age": 25, "status": "active"}
2) {"_id": "6a240c7b5da17d872dc0e103", "name": "Carol", "age": 35, "status": "inactive"}
3) {"_id": "6a240c875da17d872dc0e104", "name": "Henry", "age": 31, "scores": [75, 100, 100]}

Query with filter:

BUCKET.QUERY users '{"name": "Alice"}'

Query with sorting:

BUCKET.QUERY users '{}' SORTBY age DESC

Query with limit:

BUCKET.QUERY users '{"status": "active"}' LIMIT 10

Query with sorting and limit:

BUCKET.QUERY users '{"status": "active"}' SORTBY age ASC LIMIT 5

Query with projection:

BUCKET.QUERY users '{"status": "active"}' PROJECTION '{"name": 1, "email": 1}'

Query with in-memory result sort (no index required):

BUCKET.QUERY users '{"status": "active"}' RESULTSORT score ASC LIMIT 10

Query with collation override:

BUCKET.QUERY users '{"name": "alice"}' COLLATION '{"locale": "en", "strength": 2}'

This performs a case-insensitive match using English locale rules, regardless of the index’s collation setting.

Pagination:

> BUCKET.QUERY users '{}' LIMIT 100
1# "cursor_id" => (integer) 1
2# "entries" => [...] (first 100 documents)
> BUCKET.ADVANCE QUERY 1
1# "cursor_id" => (integer) 1
2# "entries" => [...] (next batch of documents)