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

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.new

query = firestore.col "cities"

# Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count

aggregate_query.get do |aggregate_snapshot|
  puts aggregate_snapshot.get
end

Alias an aggregate query

require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

# Create a query
query = firestore.col "cities"

# Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count aggregate_alias: 'total_cities'

aggregate_query.get do |aggregate_snapshot|
  puts aggregate_snapshot.get('total_cities')
end

Methods

#add_count

def add_count(aggregate_alias: nil) -> AggregateQuery

Adds a count aggregate.

Parameter
  • Alias (aggregate_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.new

query = firestore.col "cities"

# Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count

aggregate_query.get do |aggregate_snapshot|
  puts aggregate_snapshot.get
end

#get

def get() { |An| ... } -> Enumerator<AggregateQuerySnapshot>

Retrieves aggregate snapshot for the query.

Yields
  • (snapshot) — The block for accessing the aggregate query snapshots.
Yield Parameter
Returns
Example
require "google/cloud/firestore"

firestore = Google::Cloud::Firestore.new

query = firestore.col "cities"

# Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count

aggregate_query.get do |aggregate_snapshot|
  puts aggregate_snapshot.get
end