Runs an aggregation query.
Rather than producing Document
results like Firestore.RunQuery
, this API allows running an aggregation to produce a series of AggregationResult
server-side.
High-Level Example:
-- Return the number of documents in table given a filter.
SELECT COUNT(*) FROM ( SELECT * FROM k where a = true );
HTTP request
POST https://firestore.googleapis.com/v1beta1/{parent=projects/*/databases/*/documents}:runAggregationQuery
The URL uses gRPC Transcoding syntax.
Path parameters
Parameters | |
---|---|
parent |
Required. The parent resource name. In the format: |
Request body
The request body contains data with the following structure:
JSON representation |
---|
{ // Union field |
Fields | |
---|---|
Union field query_type . The query to run. query_type can be only one of the following: |
|
structuredAggregationQuery |
An aggregation query. |
Union field consistency_selector . The consistency mode for the query, defaults to strong consistency. consistency_selector can be only one of the following: |
|
transaction |
Run the aggregation within an already active transaction. The value here is the opaque transaction ID to execute the query in. A base64-encoded string. |
newTransaction |
Starts a new transaction as part of the query, defaulting to read-only. The new transaction ID will be returned as the first response in the stream. |
readTime |
Executes the query at the given timestamp. Requires:
A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. Examples: |
Response body
If successful, the response body contains data with the following structure:
The response for Firestore.RunAggregationQuery
.
JSON representation |
---|
{
"result": {
object ( |
Fields | |
---|---|
result |
A single aggregation result. Not present when reporting partial progress. |
transaction |
The transaction that was started as part of this request. Only present on the first response when the request requested to start a new transaction. A base64-encoded string. |
readTime |
The time at which the aggregate value is valid for. A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. Examples: |
Authorization Scopes
Requires one of the following OAuth scopes:
https://www.googleapis.com/auth/datastore
https://www.googleapis.com/auth/cloud-platform
For more information, see the Authentication Overview.
StructuredAggregationQuery
Firestore query for running an aggregation over a StructuredQuery
.
JSON representation |
---|
{ "aggregations": [ { object ( |
Fields | |
---|---|
aggregations[] |
Optional. Series of aggregations to apply over the results of the Requires:
|
Union field query_type . The base query to aggregate over. query_type can be only one of the following: |
|
structuredQuery |
Nested structured query. |
Aggregation
Defines a aggregation that produces a single result.
JSON representation |
---|
{ "alias": string, // Union field |
Fields | |
---|---|
alias |
Optional. Optional name of the field to store the result of the aggregation into. If not provided, Firestore will pick a default name following the format
becomes:
Requires:
|
Union field operator . The type of aggregation to perform, required. operator can be only one of the following: |
|
count |
Count aggregator. |
Count
Count of documents that match the query.
The COUNT(*)
aggregation function operates on the entire document so it does not require a field reference.
JSON representation |
---|
{ "upTo": string } |
Fields | |
---|---|
upTo |
Optional. Optional constraint on the maximum number of documents to count. This provides a way to set an upper bound on the number of documents to scan, limiting latency and cost. Unspecified is interpreted as no bound. High-Level Example:
Requires:
|
AggregationResult
The result of a single bucket from a Firestore aggregation query.
The keys of aggregateFields
are the same for all results in an aggregation query, unlike document queries which can have different fields present for each result.
JSON representation |
---|
{
"aggregateFields": {
string: {
object ( |
Fields | |
---|---|
aggregateFields |
The result of the aggregation functions, ex: The key is the An object containing a list of |