Send feedback
Firestore in Datastore mode API - Class Google::Cloud::Datastore::AggregateQuery (v2.13.0)
Stay organized with collections
Save and categorize content based on your preferences.
Version latestkeyboard_arrow_down
Reference documentation and code samples for the Firestore in Datastore mode API class Google::Cloud::Datastore::AggregateQuery.
AggregateQuery
An aggregate query can be used to fetch aggregate values (ex: count) for a query
Examples
require "google/cloud/datastore"
datastore = Google :: Cloud :: Datastore . new
query = Google :: Cloud :: Datastore :: Query . new
query . kind ( "Task" )
. where ( "done" , "=" , false )
Create an aggregate query
aggregate_query = query . aggregate_query
. add_count
aggregate_query_results = dataset . run_aggregation aggregate_query
puts aggregate_query_results . get
Alias an aggregate query
require "google/cloud/datastore"
datastore = Google :: Cloud :: Datastore . new
query = Google :: Cloud :: Datastore :: Query . new
query . kind ( "Task" )
. where ( "done" , "=" , false )
Create an aggregate query
aggregate_query = query . aggregate_query
. add_count aggregate_alias : 'total'
aggregate_query_results = dataset . run_aggregation aggregate_query
puts aggregate_query_results . get ( 'total' )
Methods
#add_avg
def add_avg ( name , aggregate_alias : nil ) - > AggregateQuery
Adds an average aggregate.
Parameters
name (String) — The property to apply average on.
aggregate_alias (String) (defaults to: nil) — Alias to refer to the aggregate. Optional
Returns
(AggregateQuery ) — The modified aggregate query object with the added AVG aggregate.
Examples
require "google/cloud/datastore"
datastore = Google :: Cloud :: Datastore . new
query = Google :: Cloud :: Datastore :: Query . new
query . kind ( "Task" )
. where ( "done" , "=" , false )
Create an aggregate query
aggregate_query = query . aggregate_query
. add_avg ( "score" )
aggregate_query_results = dataset . run_aggregation aggregate_query
puts aggregate_query_results . get
Alias an aggregate AVG query
require "google/cloud/datastore"
datastore = Google :: Cloud :: Datastore . new
query = Google :: Cloud :: Datastore :: Query . new
query . kind ( "Task" )
. where ( "done" , "=" , false )
# Create an aggregate query
aggregate_query = query . aggregate_query
. add_avg ( "score" , aggregate_alias : 'avg_score' )
aggregate_query_results = dataset . run_aggregation aggregate_query
puts aggregate_query_results . get ( 'avg_score' )
#add_count
def add_count ( aggregate_alias : nil ) - > AggregateQuery
Parameter
aggregate_alias (String) (defaults to: nil) — Alias to refer to the aggregate. Optional
Returns
(AggregateQuery ) — The modified aggregate query object with the added count aggregate.
Examples
require "google/cloud/datastore"
datastore = Google :: Cloud :: Datastore . new
query = Google :: Cloud :: Datastore :: Query . new
query . kind ( "Task" )
. where ( "done" , "=" , false )
Create an aggregate query
aggregate_query = query . aggregate_query
. add_count
aggregate_query_results = dataset . run_aggregation aggregate_query
puts aggregate_query_results . get
Alias an aggregate query
require "google/cloud/datastore"
datastore = Google :: Cloud :: Datastore . new
query = Google :: Cloud :: Datastore :: Query . new
query . kind ( "Task" )
. where ( "done" , "=" , false )
Create an aggregate query
aggregate_query = query . aggregate_query
. add_count aggregate_alias : 'total'
aggregate_query_results = dataset . run_aggregation aggregate_query
puts aggregate_query_results . get ( 'total' )
#add_sum
def add_sum ( name , aggregate_alias : nil ) - > AggregateQuery
Parameters
name (String) — The property to sum by.
aggregate_alias (String) (defaults to: nil) — Alias to refer to the aggregate. Optional
Returns
(AggregateQuery ) — The modified aggregate query object with the added SUM aggregate.
Examples
require "google/cloud/datastore"
datastore = Google :: Cloud :: Datastore . new
query = Google :: Cloud :: Datastore :: Query . new
query . kind ( "Task" )
. where ( "done" , "=" , false )
Create an aggregate query
aggregate_query = query . aggregate_query
. add_sum ( "score" )
aggregate_query_results = dataset . run_aggregation aggregate_query
puts aggregate_query_results . get
Alias an aggregate SUM query
require "google/cloud/datastore"
datastore = Google :: Cloud :: Datastore . new
query = Google :: Cloud :: Datastore :: Query . new
query . kind ( "Task" )
. where ( "done" , "=" , false )
# Create an aggregate query
aggregate_query = query . aggregate_query
. add_sum ( "score" , aggregate_alias : 'total_score' )
aggregate_query_results = dataset . run_aggregation aggregate_query
puts aggregate_query_results . get ( 'total_score' )
Send feedback
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License , and code samples are licensed under the Apache 2.0 License . For details, see the Google Developers Site Policies . Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-28 UTC.
Need to tell us more?
[[["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-08-28 UTC."],[],[],null,["# Firestore in Datastore mode API - Class Google::Cloud::Datastore::AggregateQuery (v2.13.0)\n\nVersion latestkeyboard_arrow_down\n\n- [2.13.0 (latest)](/ruby/docs/reference/google-cloud-datastore/latest/Google-Cloud-Datastore-AggregateQuery)\n- [2.12.0](/ruby/docs/reference/google-cloud-datastore/2.12.0/Google-Cloud-Datastore-AggregateQuery)\n- [2.11.0](/ruby/docs/reference/google-cloud-datastore/2.11.0/Google-Cloud-Datastore-AggregateQuery)\n- [2.10.0](/ruby/docs/reference/google-cloud-datastore/2.10.0/Google-Cloud-Datastore-AggregateQuery)\n- [2.9.0](/ruby/docs/reference/google-cloud-datastore/2.9.0/Google-Cloud-Datastore-AggregateQuery)\n- [2.8.0](/ruby/docs/reference/google-cloud-datastore/2.8.0/Google-Cloud-Datastore-AggregateQuery)\n- [2.7.1](/ruby/docs/reference/google-cloud-datastore/2.7.1/Google-Cloud-Datastore-AggregateQuery)\n- [2.6.0](/ruby/docs/reference/google-cloud-datastore/2.6.0/Google-Cloud-Datastore-AggregateQuery)\n- [2.5.0](/ruby/docs/reference/google-cloud-datastore/2.5.0/Google-Cloud-Datastore-AggregateQuery)\n- [2.4.0](/ruby/docs/reference/google-cloud-datastore/2.4.0/Google-Cloud-Datastore-AggregateQuery)\n- [2.3.1](/ruby/docs/reference/google-cloud-datastore/2.3.1/Google-Cloud-Datastore-AggregateQuery)\n- [2.2.4](/ruby/docs/reference/google-cloud-datastore/2.2.4/Google-Cloud-Datastore-AggregateQuery) \nReference documentation and code samples for the Firestore in Datastore mode API class Google::Cloud::Datastore::AggregateQuery.\n\nAggregateQuery\n--------------\n\nAn aggregate query can be used to fetch aggregate values (ex: count) for a query \n\nInherits\n--------\n\n- Object\n\nExamples\n--------\n\n```ruby\nrequire \"google/cloud/datastore\"\n\ndatastore = Google::Cloud::Datastore.new\n\nquery = Google::Cloud::Datastore::Query.new\nquery.kind(\"Task\")\n .where(\"done\", \"=\", false)\n\nCreate an aggregate query\naggregate_query = query.aggregate_query\n .add_count\n\naggregate_query_results = dataset.run_aggregation aggregate_query\nputs aggregate_query_results.get\n```\n\nAlias an aggregate query \n\n```ruby\nrequire \"google/cloud/datastore\"\n\ndatastore = Google::Cloud::Datastore.new\n\nquery = Google::Cloud::Datastore::Query.new\nquery.kind(\"Task\")\n .where(\"done\", \"=\", false)\n\nCreate an aggregate query\naggregate_query = query.aggregate_query\n .add_count aggregate_alias: 'total'\n\naggregate_query_results = dataset.run_aggregation aggregate_query\nputs aggregate_query_results.get('total')\n```\n\nMethods\n-------\n\n### #add_avg\n\n def add_avg(name, aggregate_alias: nil) -\u003e AggregateQuery\n\nAdds an average aggregate. \n**Parameters**\n\n- **name** (String) --- The property to apply average on.\n- **aggregate_alias** (String) *(defaults to: nil)* --- Alias to refer to the aggregate. Optional \n**Returns**\n\n- ([AggregateQuery](./Google-Cloud-Datastore-AggregateQuery)) --- The modified aggregate query object with the added AVG aggregate.\n**Examples** \n\n```ruby\nrequire \"google/cloud/datastore\"\n\ndatastore = Google::Cloud::Datastore.new\n\nquery = Google::Cloud::Datastore::Query.new\nquery.kind(\"Task\")\n .where(\"done\", \"=\", false)\n\nCreate an aggregate query\naggregate_query = query.aggregate_query\n .add_avg(\"score\")\n\naggregate_query_results = dataset.run_aggregation aggregate_query\nputs aggregate_query_results.get\n```\n\nAlias an aggregate AVG query \n\n```ruby\nrequire \"google/cloud/datastore\"\n\ndatastore = Google::Cloud::Datastore.new\n\nquery = Google::Cloud::Datastore::Query.new\nquery.kind(\"Task\")\n .where(\"done\", \"=\", false)\n\n# Create an aggregate query\naggregate_query = query.aggregate_query\n .add_avg(\"score\", aggregate_alias: 'avg_score')\n\naggregate_query_results = dataset.run_aggregation aggregate_query\nputs aggregate_query_results.get('avg_score')\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-Datastore-AggregateQuery)) --- The modified aggregate query object with the added count aggregate.\n**Examples** \n\n```ruby\nrequire \"google/cloud/datastore\"\n\ndatastore = Google::Cloud::Datastore.new\n\nquery = Google::Cloud::Datastore::Query.new\nquery.kind(\"Task\")\n .where(\"done\", \"=\", false)\n\nCreate an aggregate query\naggregate_query = query.aggregate_query\n .add_count\n\naggregate_query_results = dataset.run_aggregation aggregate_query\nputs aggregate_query_results.get\n```\n\nAlias an aggregate query \n\n```ruby\nrequire \"google/cloud/datastore\"\n\ndatastore = Google::Cloud::Datastore.new\n\nquery = Google::Cloud::Datastore::Query.new\nquery.kind(\"Task\")\n .where(\"done\", \"=\", false)\n\nCreate an aggregate query\naggregate_query = query.aggregate_query\n .add_count aggregate_alias: 'total'\n\naggregate_query_results = dataset.run_aggregation aggregate_query\nputs aggregate_query_results.get('total')\n```\n\n### #add_sum\n\n def add_sum(name, aggregate_alias: nil) -\u003e AggregateQuery\n\nAdds a sum aggregate. \n**Parameters**\n\n- **name** (String) --- The property to sum by.\n- **aggregate_alias** (String) *(defaults to: nil)* --- Alias to refer to the aggregate. Optional \n**Returns**\n\n- ([AggregateQuery](./Google-Cloud-Datastore-AggregateQuery)) --- The modified aggregate query object with the added SUM aggregate.\n**Examples** \n\n```ruby\nrequire \"google/cloud/datastore\"\n\ndatastore = Google::Cloud::Datastore.new\n\nquery = Google::Cloud::Datastore::Query.new\nquery.kind(\"Task\")\n .where(\"done\", \"=\", false)\n\nCreate an aggregate query\naggregate_query = query.aggregate_query\n .add_sum(\"score\")\n\naggregate_query_results = dataset.run_aggregation aggregate_query\nputs aggregate_query_results.get\n```\n\nAlias an aggregate SUM query \n\n```ruby\nrequire \"google/cloud/datastore\"\n\ndatastore = Google::Cloud::Datastore.new\n\nquery = Google::Cloud::Datastore::Query.new\nquery.kind(\"Task\")\n .where(\"done\", \"=\", false)\n\n# Create an aggregate query\naggregate_query = query.aggregate_query\n .add_sum(\"score\", aggregate_alias: 'total_score')\n\naggregate_query_results = dataset.run_aggregation aggregate_query\nputs aggregate_query_results.get('total_score')\n```"]]