Firestore in Datastore mode API - Class Google::Cloud::Datastore::Dataset::AggregateQueryResults (v2.13.0)

Reference documentation and code samples for the Firestore in Datastore mode API class Google::Cloud::Datastore::Dataset::AggregateQueryResults.

AggregateQueryResults

An AggregateQueryResult object is a representation for a result of an AggregateQuery or a GqlQuery.

Inherits

  • Object

Example

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

Methods

#aggregate_fields

def aggregate_fields() -> Hash{String => Integer, Float}

The result of the aggregation query, returned as a hash of key-value pairs. The key is the alias of the aggregate function, and the value is the result of the aggregation.

The alias of the aggregate function can be:

  • an aggregate literal "sum", "avg", or "count"
  • a custom aggregate alias
Returns
  • (Hash{String => Integer, Float})

#explain_metrics

def explain_metrics() -> Google::Cloud::Datastore::V1::ExplainMetrics, nil

Query explain metrics. This is only present when the explain_options are provided to #run_aggregation. It is sent only once with the response.

Returns
  • (Google::Cloud::Datastore::V1::ExplainMetrics, nil)

#explain_options

def explain_options() -> Google::Cloud::Datastore::V1::ExplainOptions, nil

The options for query explanation.

This is a copy of the input parameter supplied to the #run_aggregation function.

Returns
  • (Google::Cloud::Datastore::V1::ExplainOptions, nil)

#get

def get(aggregate_alias = nil) -> Integer, Float, nil

Retrieves the aggregate data.

if the aggregate_alias does not exist.

Parameter
  • aggregate_alias (String) — The alias used to access the aggregate value. For an AggregateQuery with a single aggregate field, this parameter can be omitted.
Returns
  • (Integer, Float, nil) — The aggregate value. Returns nil
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')

#read_time

def read_time() -> Google::Protobuf::Timestamp

The time when the query was executed.

Returns
  • (Google::Protobuf::Timestamp)