Query operations

Read data: SQL, typed chain queries, counts and aggregates.

Docs menu

data

data.query

Run a typed query over one collection: a where filter, sort, limit and offset (see the schema); use_index false forces a scan. Answers the matching records. For joins, grouping and aggregates use query.chain; for SQL use query.sql.

Route
POST /v1/{collection}/_query
GET /v1/{collection}/_query
ANY query
Auth
App key or user session; your security rules apply none
MCP tool
substrio_query
CLI
substrio op data.query

Parameters

NameInTypeRequiredDescription
collectionpathstringYesThe collection's name, as collection.list names it.
use_indexquerybooleanNofalse runs this one query without the collection's index (a scan, never cached), to explain or debug a result against the indexed one.

Response

A JSON object.

Errors

A failed call answers the error envelope with a typed code.

Example

curl -X POST "$SUBSTRIO_URL/v1/$COLLECTION/_query" \
  -H "Authorization: Bearer $SUBSTRIO_KEY"

query

query.aggregate

Run an aggregation pipeline over one collection: stages run in order, one operator per stage (see the schema); use_index false forces a scan. For SQL-style grouping with having use query.chain.

Route
POST /v1/{collection}/_aggregate
ANY aggregate
Auth
App key tenant
MCP tool
substrio_pipeline
CLI
substrio op query.aggregate

Parameters

NameInTypeRequiredDescription
collectionpathstringYesThe collection's name, as collection.list names it.
use_indexquerybooleanNofalse runs this one query without the collection's index (a scan, never cached), to explain or debug a result against the indexed one.

Response

A JSON object.

Errors

A failed call answers the error envelope with a typed code.

Example

curl -X POST "$SUBSTRIO_URL/v1/$COLLECTION/_aggregate" \
  -H "Authorization: Bearer $SUBSTRIO_KEY"

query.chain

Run a typed chain query over a collection: the JS chain's methods as JSON (conditions eq, neq, gt, gte, lt, lte, in, nin, iin, ieq, exists, startsWith, istartsWith, endsWith, contains, icontains (served by a declared keyword index), like, ilike, regex, iregex, size, sizeGt, sizeLt, containsAll, containsAny, between, eqField and the other field comparisons (value names the other field), inQuery, notInQuery, existsQuery, and, or, not; elemMatch is JS only; joins of kind first, inner or left; sort keys, skip, limit, select, distinct; group_by with aggregates and having; compound union, unionAll, intersect, except; terminals all, first, count, ids, sum, avg, min, max). A field may be alias.field of a join. A grouped, aggregated, distinct or compound plan answers plain rows as sql does (rows, columns).

Route
POST /v1/_chain
Auth
App key tenant
MCP tool
substrio_query_chain
CLI
substrio op query.chain

Parameters

NameInTypeRequiredDescription
collectionbodystringYesthe collection to read.
wherebodyanyNoone condition node, or an array of nodes that are ANDed: a condition node {op, field, value} (value: null, boolean, number or string; an array for in and nin; [low, high] for between; optional boolean for exists), {op: inQuery|notInQuery, field, query, queryField}, {op: existsQuery, query} or {op: and|or|not, conditions: [node, ...]}.
sortbodyanyNoone sort key, or an array of keys (the first is primary; several keys answer by SQL's order: nulls last, ties in row order).
sort_bybodyobjectNoalias of sort.
group_bybodyanyNoa field name: sum, avg, min, max and count answer per value of this field; or an array of field names (alias.field allowed): the group keys, with aggregates, answering plain rows.
aggregatesbodyarray of objectNothe aggregates of an array group_by, one result column each.
havingbodyanyNoconditions over group keys and aggregate aliases, ANDed: {op: eq|neq|gt|gte|lt|lte (or = != > >= < <=), field, value}, or an array of them.
distinctbodybooleanNoeach row of select once (answers plain rows).
compoundbodyarray of objectNothis chain's rows and each arm's, paired by position; the chain's sort, skip and limit apply to the whole; an arm has no sort, skip, limit or compound of its own.
skipbodyintegerNoMinimum 0.
limitbodyintegerNoMinimum 0.
selectbodyarray of stringNothe fields each document carries (alias.field of a join answers plain rows).
joinsbodyarray of objectNo
terminalbody"all" | "first" | "count" | "ids" | "sum" | "avg" | "min" | "max"Nowhat the chain ends in (default all).
fieldbodystringNothe field sum, avg, min and max fold.

Response

FieldTypePresentDescription
documentsarray of objectWhen set
documentobject or nullWhen set
idsarray of stringWhen set
countintegerWhen set
matchedintegerWhen set
valuenumber or nullWhen set
groupsarray of objectWhen set

Errors

A failed call answers the error envelope with a typed code.

Example

curl -X POST "$SUBSTRIO_URL/v1/_chain" \
  -H "Authorization: Bearer $SUBSTRIO_KEY" \
  -H "Content-Type: application/json" \
  -d '{"collection":"collection"}'

query.count

Count the records of a collection that match a where filter, without returning them; use_index false forces a scan.

Route
GET /v1/{collection}/_count
ANY count
Auth
App key tenant
MCP tool
substrio_count
CLI
substrio op query.count

Parameters

NameInTypeRequiredDescription
collectionpathstringYesThe collection's name, as collection.list names it.
use_indexquerybooleanNofalse runs this one query without the collection's index (a scan, never cached), to explain or debug a result against the indexed one.

Response

A JSON object.

Errors

A failed call answers the error envelope with a typed code.

Example

curl "$SUBSTRIO_URL/v1/$COLLECTION/_count" \
  -H "Authorization: Bearer $SUBSTRIO_KEY"

query.multi

Run several queries over different collections in one call and answer each result in order.

Route
POST /v1/_multi_search
Auth
App key or user session; your security rules apply none
MCP tool
substrio_multi_search
CLI
substrio op query.multi

Parameters

NameInTypeRequiredDescription
collectionsbodyarray of stringYes
querybodystringYes
limitbodyintegerNo10 when absent.
search_kbodyintegerNolimit times 2 when absent.
modebodystringNo

Response

FieldTypePresentDescription
dataarray of objectAlways
countintegerAlways
total_searchedintegerAlways
collections_searchedarray of stringAlways
collections_skippedarray of stringAlways

Errors

A failed call answers the error envelope with a typed code.

Example

curl -X POST "$SUBSTRIO_URL/v1/_multi_search" \
  -H "Authorization: Bearer $SUBSTRIO_KEY" \
  -H "Content-Type: application/json" \
  -d '{"collections":[],"query":"query"}'

query.qx

Execute a parser-owned query plan.

Route
POST /v1/{collection}/_qx
Auth
App key or user session; your security rules apply none
CLI
substrio op query.qx

Parameters

NameInTypeRequiredDescription
collectionpathstringYesThe collection's name, as collection.list names it.

Response

A JSON object.

Errors

A failed call answers the error envelope with a typed code.

Example

curl -X POST "$SUBSTRIO_URL/v1/$COLLECTION/_qx" \
  -H "Authorization: Bearer $SUBSTRIO_KEY"

query.sql

Run one SQL statement with bound parameters.

Route
POST /v1/_sql
Auth
App key tenant
MCP tool
substrio_sql
CLI
substrio op query.sql

Parameters

NameInTypeRequiredDescription
sqlbodystringYesone SQL statement; ? marks a bound parameter.
paramsbodyarray of string or number or boolean or nullNovalues bound to the placeholders in order.
tablebodystringNothe table a SELECT with no FROM clause reads.
limitbodyintegerNothe most rows per page (default 1000); more rows answer next_cursor. Minimum 1.
cursorbodystringNothe previous page's next_cursor, sent with the same statement and parameters; it reads the result the first page ran (no re-run), held until its last page is read or 5 idle minutes pass, then CURSOR_INVALIDATED.

Response

FieldTypePresentDescription
returns_rowsbooleanAlways
columnsarray of objectAlways
rowsarray of array of string or number or boolean or nullAlways
offsetintegerAlways
total_rowsintegerAlways
next_cursorstring or nullAlways
changesintegerAlways
last_insert_idintegerAlways

Errors

A failed call answers the error envelope with a typed code.

Example

curl -X POST "$SUBSTRIO_URL/v1/_sql" \
  -H "Authorization: Bearer $SUBSTRIO_KEY" \
  -H "Content-Type: application/json" \
  -d '{"sql":"sql"}'

query.stream

Stream typed query results over WebSocket.

Route
GET /v1/query/ws
Auth
App key or user session; your security rules apply none
CLI
substrio op query.stream

Parameters

NameInTypeRequiredDescription
collectionquerystringYes
wherequeryobjectNo
selectqueryarrayNo
limitqueryintegerNoMinimum 1.
skipqueryintegerNoMinimum 0.
sortqueryobjectNo

Response

A JSON object.

Errors

A failed call answers the error envelope with a typed code.

Example

curl "$SUBSTRIO_URL/v1/query/ws" \
  -H "Authorization: Bearer $SUBSTRIO_KEY"

sql

sql.batch

Run SQL statements with bound parameters in order in one transaction; a failure rolls every row write back and names the failing statement's index, errno, sqlstate and message.

Route
POST /v1/_sql/batch
Auth
App key tenant
MCP tool
substrio_sql_batch
CLI
substrio op sql.batch

Parameters

NameInTypeRequiredDescription
statementsbodyarray of objectYesstatements run in order in one transaction on one connection; the first failure rolls every earlier one back and names its index.

Response

FieldTypePresentDescription
resultsarray of objectAlways
countintegerAlways

Errors

A failed call answers the error envelope with a typed code.

Example

curl -X POST "$SUBSTRIO_URL/v1/_sql/batch" \
  -H "Authorization: Bearer $SUBSTRIO_KEY" \
  -H "Content-Type: application/json" \
  -d '{"statements":[]}'