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_avg(field, aggregate_alias: nil) ⇒ AggregateQuery

Adds an average 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_avg("population")

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

Parameters:

  • field (String)

    The field to apply average on

  • aggregate_alias (String) (defaults to: nil)

    Alias to refer to the aggregate

Returns:

  • (AggregateQuery)

    A new aggregate query with the added average aggregate.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/google/cloud/firestore/aggregate_query.rb', line 192

def add_avg field, aggregate_alias: nil
  aggregate_alias ||= DEFAULT_AVG_ALIAS
  field = FieldPath.parse field unless field.is_a? FieldPath
  new_aggregate = Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation.new(
    avg: Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation::Avg.new(
      field: Google::Cloud::Firestore::V1::StructuredQuery::FieldReference.new(
        field_path: field.formatted_string
      )
    ),
    alias: aggregate_alias
  )

  start new_aggregate
end

#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:

  • aggregate_alias (String) (defaults to: nil)

    Alias to refer to the aggregate. Optional

Returns:

  • (AggregateQuery)

    A new aggregate query with the added count aggregate.



121
122
123
124
125
126
127
128
129
# File 'lib/google/cloud/firestore/aggregate_query.rb', line 121

def add_count aggregate_alias: nil
  aggregate_alias ||= DEFAULT_COUNT_ALIAS
  new_aggregate = Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation.new(
    count: Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation::Count.new,
    alias: aggregate_alias
  )

  start new_aggregate
end

#add_sum(field, aggregate_alias: nil) ⇒ AggregateQuery

Adds a sum 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_sum("population")

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

Parameters:

  • field (String)

    The field to sum by

  • aggregate_alias (String) (defaults to: nil)

    Alias to refer to the aggregate

Returns:

  • (AggregateQuery)

    A new aggregate query with the added sum aggregate.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/google/cloud/firestore/aggregate_query.rb', line 154

def add_sum field, aggregate_alias: nil
  aggregate_alias ||= DEFAULT_SUM_ALIAS
  field = FieldPath.parse field unless field.is_a? FieldPath
  new_aggregate = Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation.new(
    sum: Google::Cloud::Firestore::V1::StructuredAggregationQuery::Aggregation::Sum.new(
      field: Google::Cloud::Firestore::V1::StructuredQuery::FieldReference.new(
        field_path: field.formatted_string
      )
    ),
    alias: aggregate_alias
  )

  start new_aggregate
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:



243
244
245
246
247
248
249
250
251
252
253
# File 'lib/google/cloud/firestore/aggregate_query.rb', line 243

def get
  ensure_service!

  return enum_for :get unless block_given?

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