Class: Google::Cloud::Firestore::AggregateQuery
- Inherits:
-
Object
- Object
- Google::Cloud::Firestore::AggregateQuery
- 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.
Instance Method Summary collapse
-
#add_avg(field, aggregate_alias: nil) ⇒ AggregateQuery
Adds an average aggregate.
-
#add_count(aggregate_alias: nil) ⇒ AggregateQuery
Adds a count aggregate.
-
#add_sum(field, aggregate_alias: nil) ⇒ AggregateQuery
Adds a sum aggregate.
-
#get {|snapshot| ... } ⇒ Enumerator<AggregateQuerySnapshot>
Retrieves aggregate snapshot for the query.
Instance Method Details
#add_avg(field, aggregate_alias: nil) ⇒ AggregateQuery
Adds an 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.
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.
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.
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 |