Firestore in Datastore mode API - Class Google::Cloud::Datastore::AggregateQuery (v2.4.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_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')