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_avg
def add_avg(field, aggregate_alias: nil) -> AggregateQuery
Adds an average aggregate.
- field (String) — The field to apply average on
- aggregate_alias (String) (defaults to: nil) — Alias to refer to the aggregate
- (AggregateQuery) — A new aggregate query with the added average aggregate.
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new query = firestore.col "cities" # Create an aggregate query aggregate_query = query.aggregate_query .add_avg("population") aggregate_query.get do |aggregate_snapshot| puts aggregate_snapshot.get end
#add_count
def add_count(aggregate_alias: nil) -> AggregateQuery
Adds a count aggregate.
- aggregate_alias (String) (defaults to: nil) — Alias to refer to the aggregate. Optional
- (AggregateQuery) — A new aggregate query with the added count aggregate.
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
#add_sum
def add_sum(field, aggregate_alias: nil) -> AggregateQuery
Adds a sum aggregate.
- field (String) — The field to sum by
- aggregate_alias (String) (defaults to: nil) — Alias to refer to the aggregate
- (AggregateQuery) — A new aggregate query with the added sum aggregate.
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new query = firestore.col "cities" # Create an aggregate query aggregate_query = query.aggregate_query .add_sum("population") aggregate_query.get do |aggregate_snapshot| puts aggregate_snapshot.get end
#get
def get() { |An| ... } -> Enumerator<AggregateQuerySnapshot>
Retrieves aggregate snapshot for the query.
- (snapshot) — The block for accessing the aggregate query snapshots.
- An (AggregateQuerySnapshot) — aggregate query snapshot.
- (Enumerator<AggregateQuerySnapshot>) — A list of aggregate query snapshots.
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