Reference documentation and code samples for the Cloud Firestore API class Google::Cloud::Firestore::AggregateQuery.
AggregateQuery
An aggregate query can be used to fetch aggregate values (ex: count) for a query
Instances of this class are immutable. All methods that refine the aggregate query
return new instances.
Inherits
Object
Examples
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.newquery=firestore.col"cities"# Create an aggregate queryaggregate_query=query.aggregate_query.add_countaggregate_query.getdo|aggregate_snapshot|putsaggregate_snapshot.getend
Alias an aggregate query
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Create a queryquery=firestore.col"cities"# Create an aggregate queryaggregate_query=query.aggregate_query.add_countaggregate_alias:'total_cities'aggregate_query.getdo|aggregate_snapshot|putsaggregate_snapshot.get('total_cities')end
aggregate_alias (String) (defaults to: nil) — Alias to refer to the aggregate
Returns
(AggregateQuery) — A new aggregate query with the added average aggregate.
Example
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.newquery=firestore.col"cities"# Create an aggregate queryaggregate_query=query.aggregate_query.add_avg("population")aggregate_query.getdo|aggregate_snapshot|putsaggregate_snapshot.getend
#add_count
defadd_count(aggregate_alias:nil)->AggregateQuery
Adds a count aggregate.
Parameter
aggregate_alias (String) (defaults to: nil) — Alias to refer to the aggregate. Optional
Returns
(AggregateQuery) — A new aggregate query with the added count aggregate.
Example
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.newquery=firestore.col"cities"# Create an aggregate queryaggregate_query=query.aggregate_query.add_countaggregate_query.getdo|aggregate_snapshot|putsaggregate_snapshot.getend
aggregate_alias (String) (defaults to: nil) — Alias to refer to the aggregate
Returns
(AggregateQuery) — A new aggregate query with the added sum aggregate.
Example
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.newquery=firestore.col"cities"# Create an aggregate queryaggregate_query=query.aggregate_query.add_sum("population")aggregate_query.getdo|aggregate_snapshot|putsaggregate_snapshot.getend
Retrieves the query explanation for the aggregate query.
By default, the query is only planned, not executed, returning only metrics from the
planning stages. If analyze is set to true the query will be planned and executed,
returning the AggregateQuerySnapshot alongside both planning and execution stage metrics.
Unlike the enumerator returned from AggregateQuery#get, the AggregateQueryExplainResult
caches its snapshot and metrics after the first access.
Parameter
analyze (Boolean) (defaults to: false) — Whether to execute the query and return the execution stage metrics
in addition to planning metrics.
If set to false the query will be planned only and will return planning
stage metrics without results.
If set to true the query will be executed, and will return the query results,
planning stage metrics, and execution stage metrics.
Defaults to false.
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.newquery=firestore.col"cities"# Create an aggregate queryaggregate_query=query.aggregate_query.add_countaggregate_query.getdo|aggregate_snapshot|putsaggregate_snapshot.getend
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-09 UTC."],[],[],null,["# Cloud Firestore API - Class Google::Cloud::Firestore::AggregateQuery (v3.1.0)\n\nVersion latestkeyboard_arrow_down\n\n- [3.1.0 (latest)](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-AggregateQuery)\n- [3.0.0](/ruby/docs/reference/google-cloud-firestore/3.0.0/Google-Cloud-Firestore-AggregateQuery)\n- [2.16.1](/ruby/docs/reference/google-cloud-firestore/2.16.1/Google-Cloud-Firestore-AggregateQuery)\n- [2.15.1](/ruby/docs/reference/google-cloud-firestore/2.15.1/Google-Cloud-Firestore-AggregateQuery)\n- [2.14.0](/ruby/docs/reference/google-cloud-firestore/2.14.0/Google-Cloud-Firestore-AggregateQuery)\n- [2.13.1](/ruby/docs/reference/google-cloud-firestore/2.13.1/Google-Cloud-Firestore-AggregateQuery)\n- [2.12.0](/ruby/docs/reference/google-cloud-firestore/2.12.0/Google-Cloud-Firestore-AggregateQuery)\n- [2.11.0](/ruby/docs/reference/google-cloud-firestore/2.11.0/Google-Cloud-Firestore-AggregateQuery)\n- [2.10.1](/ruby/docs/reference/google-cloud-firestore/2.10.1/Google-Cloud-Firestore-AggregateQuery)\n- [2.9.1](/ruby/docs/reference/google-cloud-firestore/2.9.1/Google-Cloud-Firestore-AggregateQuery)\n- [2.8.0](/ruby/docs/reference/google-cloud-firestore/2.8.0/Google-Cloud-Firestore-AggregateQuery)\n- [2.7.2](/ruby/docs/reference/google-cloud-firestore/2.7.2/Google-Cloud-Firestore-AggregateQuery)\n- [2.6.6](/ruby/docs/reference/google-cloud-firestore/2.6.6/Google-Cloud-Firestore-AggregateQuery) \nReference documentation and code samples for the Cloud Firestore API class Google::Cloud::Firestore::AggregateQuery.\n\nAggregateQuery\n--------------\n\nAn aggregate query can be used to fetch aggregate values (ex: count) for a query\n\nInstances of this class are immutable. All methods that refine the aggregate query\nreturn new instances. \n\nInherits\n--------\n\n- Object\n\nExamples\n--------\n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\nquery = firestore.col \"cities\"\n\n# Create an aggregate query\naggregate_query = query.aggregate_query\n .add_count\n\naggregate_query.get do |aggregate_snapshot|\n puts aggregate_snapshot.get\nend\n```\n\nAlias an aggregate query \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Create a query\nquery = firestore.col \"cities\"\n\n# Create an aggregate query\naggregate_query = query.aggregate_query\n .add_count aggregate_alias: 'total_cities'\n\naggregate_query.get do |aggregate_snapshot|\n puts aggregate_snapshot.get('total_cities')\nend\n```\n\nMethods\n-------\n\n### #add_avg\n\n def add_avg(field, aggregate_alias: nil) -\u003e AggregateQuery\n\nAdds an average aggregate. \n**Parameters**\n\n- **field** (String) --- The field to apply average on\n- **aggregate_alias** (String) *(defaults to: nil)* --- Alias to refer to the aggregate \n**Returns**\n\n- ([AggregateQuery](./Google-Cloud-Firestore-AggregateQuery)) --- A new aggregate query with the added average aggregate.\n**Example** \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\nquery = firestore.col \"cities\"\n\n# Create an aggregate query\naggregate_query = query.aggregate_query\n .add_avg(\"population\")\n\naggregate_query.get do |aggregate_snapshot|\n puts aggregate_snapshot.get\nend\n```\n\n### #add_count\n\n def add_count(aggregate_alias: nil) -\u003e AggregateQuery\n\nAdds a count aggregate. \n**Parameter**\n\n- **aggregate_alias** (String) *(defaults to: nil)* --- Alias to refer to the aggregate. Optional \n**Returns**\n\n- ([AggregateQuery](./Google-Cloud-Firestore-AggregateQuery)) --- A new aggregate query with the added count aggregate.\n**Example** \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\nquery = firestore.col \"cities\"\n\n# Create an aggregate query\naggregate_query = query.aggregate_query\n .add_count\n\naggregate_query.get do |aggregate_snapshot|\n puts aggregate_snapshot.get\nend\n```\n\n### #add_sum\n\n def add_sum(field, aggregate_alias: nil) -\u003e AggregateQuery\n\nAdds a sum aggregate. \n**Parameters**\n\n- **field** (String) --- The field to sum by\n- **aggregate_alias** (String) *(defaults to: nil)* --- Alias to refer to the aggregate \n**Returns**\n\n- ([AggregateQuery](./Google-Cloud-Firestore-AggregateQuery)) --- A new aggregate query with the added sum aggregate.\n**Example** \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\nquery = firestore.col \"cities\"\n\n# Create an aggregate query\naggregate_query = query.aggregate_query\n .add_sum(\"population\")\n\naggregate_query.get do |aggregate_snapshot|\n puts aggregate_snapshot.get\nend\n```\n\n### #explain\n\n def explain(analyze: false) -\u003e AggregateQueryExplainResult\n\nRetrieves the query explanation for the aggregate query.\nBy default, the query is only planned, not executed, returning only metrics from the\nplanning stages. If `analyze` is set to `true` the query will be planned and executed,\nreturning the `AggregateQuerySnapshot` alongside both planning and execution stage metrics.\n\n\n\u003cbr /\u003e\n\nUnlike the enumerator returned from `AggregateQuery#get`, the `AggregateQueryExplainResult`\ncaches its snapshot and metrics after the first access. \n**Parameter**\n\n- **analyze** (Boolean) *(defaults to: false)* --- Whether to execute the query and return the execution stage metrics in addition to planning metrics. If set to `false` the query will be planned only and will return planning stage metrics without results. If set to `true` the query will be executed, and will return the query results, planning stage metrics, and execution stage metrics. Defaults to `false`. \n**Returns**\n\n- ([AggregateQueryExplainResult](./Google-Cloud-Firestore-AggregateQueryExplainResult))\n**Examples**\n\nGetting only the planning stage metrics for the aggregate query \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\nquery = firestore.col(:cities).aggregate_query.add_count\n\nexplain_result = query.explain\nmetrics = explain_result.explain_metrics\nputs \"Plan summary: #{metrics.plan_summary}\" if metrics&.plan_summary\n```\n\nGetting planning and execution stage metrics, as well as aggregate query results \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\nquery = firestore.col(:cities).aggregate_query.add_count\n\nexplain_result = query.explain analyze: true\nmetrics = explain_result.explain_metrics\nputs \"Plan summary: #{metrics.plan_summary}\" if metrics&.plan_summary\nputs \"Results returned: #{metrics.execution_stats.results_returned}\" if metrics&.execution_stats\nsnapshot = explain_result.snapshot\nputs \"Count: #{snapshot.get}\" if snapshot\n```\n\n### #get\n\n def get() { |An| ... } -\u003e Enumerator\u003cAggregateQuerySnapshot\u003e\n\nRetrieves aggregate snapshot for the query. \n**Yields**\n\n- (snapshot) --- The block for accessing the aggregate query snapshots. \n**Yield Parameter**\n\n- **An** ([AggregateQuerySnapshot](./Google-Cloud-Firestore-AggregateQuerySnapshot)) --- aggregate query snapshot. \n**Returns**\n\n- (Enumerator\\\u003c[AggregateQuerySnapshot](./Google-Cloud-Firestore-AggregateQuerySnapshot)\\\u003e) --- A list of aggregate query snapshots.\n**Example** \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\nquery = firestore.col \"cities\"\n\n# Create an aggregate query\naggregate_query = query.aggregate_query\n .add_count\n\naggregate_query.get do |aggregate_snapshot|\n puts aggregate_snapshot.get\nend\n```"]]