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/v1/{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 |
---|
{ "explainOptions": { object ( |
Fields | |
---|---|
explainOptions |
Optional. Explain options for the query. If set, additional query statistics will be returned. If not, only query results will be returned. |
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. This must be a microsecond precision timestamp within the past one hour, or if Point-in-Time Recovery is enabled, can additionally be a whole minute timestamp within the past 7 days. A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. Examples: |
Response body
The response for Firestore.RunAggregationQuery
.
If successful, the response body contains data with the following structure:
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 result was computed. This is always monotonically increasing; in this case, the previous AggregationResult in the result stream are guaranteed not to have changed between their If the query returns no results, a response with A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. Examples: |
explainMetrics |
Query explain metrics. This is only present when the |
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 an 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. |
sum |
Sum aggregator. |
avg |
Average 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:
|
Sum
Sum of the values of the requested field.
Only numeric values will be aggregated. All non-numeric values including
NULL
are skipped.If the aggregated values contain
NaN
, returnsNaN
. Infinity math follows IEEE-754 standards.If the aggregated value set is empty, returns 0.
Returns a 64-bit integer if all aggregated numbers are integers and the sum result does not overflow. Otherwise, the result is returned as a double. Note that even if all the aggregated values are integers, the result is returned as a double if it cannot fit within a 64-bit signed integer. When this occurs, the returned value will lose precision.
When underflow occurs, floating-point aggregation is non-deterministic. This means that running the same query repeatedly without any changes to the underlying values could produce slightly different results each time. In those cases, values should be stored as integers over floating-point numbers.
JSON representation |
---|
{
"field": {
object ( |
Fields | |
---|---|
field |
The field to aggregate on. |
Avg
Average of the values of the requested field.
Only numeric values will be aggregated. All non-numeric values including
NULL
are skipped.If the aggregated values contain
NaN
, returnsNaN
. Infinity math follows IEEE-754 standards.If the aggregated value set is empty, returns
NULL
.Always returns the result as a double.
JSON representation |
---|
{
"field": {
object ( |
Fields | |
---|---|
field |
The field to aggregate on. |
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 |