Class: Google::Cloud::Firestore::AggregateQuerySnapshot

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

Overview

AggregateQuerySnapshot

An aggregate query snapshot object is an immutable representation for an aggregate query result.

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

query = firestore.col "cities"

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

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

Returns:

  • (Integer)

    The aggregate value.

Instance Method Summary collapse

Instance Method Details

#get(aggregate_alias = nil) ⇒ Integer

Retrieves the aggregate data.

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

query = firestore.col "cities"

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

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

Parameters:

  • aggregate_alias (String) (defaults to: nil)

    The alias used to access the aggregate value. For an AggregateQuery with a single aggregate field, this parameter can be omitted.

Returns:

  • (Integer)

    The aggregate value.

  • (Integer)

    The aggregate value.



95
96
97
98
99
100
101
# File 'lib/google/cloud/firestore/aggregate_query_snapshot.rb', line 95

def get aggregate_alias = nil
  if @aggregate_fields.count > 1 && aggregate_alias.nil?
    raise ArgumentError, "Required param aggregate_alias for AggregateQuery with multiple aggregate fields"
  end
  aggregate_alias ||= @aggregate_fields.keys.first
  @aggregate_fields[aggregate_alias]
end