Class: Google::Cloud::Firestore::AggregateQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/firestore/aggregate_query.rb

Overview

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.

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

Instance Method Summary collapse

Instance Method Details

#add_count(aggregate_alias: nil) ⇒ AggregateQuery

Adds a count aggregate.

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

Parameters:

  • Alias (aggregate_alias)

    to refer to the aggregate. Optional

Returns:

  • (AggregateQuery)

    A new aggregate query with the added count aggregate.



108
109
110
111
112
113
114
115
116
# File 'lib/google/cloud/firestore/aggregate_query.rb', line 108

def add_count aggregate_alias: nil
  aggregate_alias ||= ALIASES[:count]
  new_aggregates = @aggregates.dup
  new_aggregates << StructuredAggregationQuery::Aggregation.new(
    count: StructuredAggregationQuery::Aggregation::Count.new,
    alias: aggregate_alias
  )
  AggregateQuery.start query, new_aggregates, parent_path, client
end

#get {|snapshot| ... } ⇒ Enumerator<AggregateQuerySnapshot>

Retrieves aggregate snapshot for the query.

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

Yields:

  • (snapshot)

    The block for accessing the aggregate query snapshots.

Yield Parameters:

Returns:



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/google/cloud/firestore/aggregate_query.rb', line 141

def get
  ensure_service!

  return enum_for :get unless block_given?

  responses = service.run_aggregate_query @parent_path, structured_aggregation_query
  responses.each do |response|
    next if response.result.nil?
    yield AggregateQuerySnapshot.from_run_aggregate_query_response response
  end
end