Firestore in Datastore mode API - Class Google::Cloud::Datastore::AggregateQuery (v2.10.0)

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

Inherits

  • Object

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

Adds a count aggregate.

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

Adds a sum aggregate.

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')