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
Example
- (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
#get
def get() { |An| ... } -> Enumerator<AggregateQuerySnapshot>
Retrieves aggregate snapshot for the query.
Yields
- (snapshot) — The block for accessing the aggregate query snapshots.
Yield Parameter
- An (AggregateQuerySnapshot) — aggregate query snapshot.
Returns
Example
- (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