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.
-
#explain(analyze: false) ⇒ AggregateQueryExplainResult
Retrieves the query explanation for the aggregate query.
-
#get {|snapshot| ... } ⇒ Enumerator<AggregateQuerySnapshot>
Retrieves aggregate snapshot for the query.
Instance Method Details
#add_avg(field, aggregate_alias: nil) ⇒ AggregateQuery
Adds an average aggregate.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/google/cloud/firestore/aggregate_query.rb', line 193 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.
122 123 124 125 126 127 128 129 130 |
# File 'lib/google/cloud/firestore/aggregate_query.rb', line 122 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.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/google/cloud/firestore/aggregate_query.rb', line 155 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 |
#explain(analyze: false) ⇒ AggregateQueryExplainResult
Retrieves the query explanation for the aggregate query.
By default, the query is only planned, not executed, returning only metrics from the
planning stages. If analyze is set to true the query will be planned and executed,
returning the AggregateQuerySnapshot alongside both planning and execution stage metrics.
Unlike the enumerator returned from AggregateQuery#get, the AggregateQueryExplainResult
caches its snapshot and metrics after the first access.
299 300 301 302 303 304 305 306 307 308 |
# File 'lib/google/cloud/firestore/aggregate_query.rb', line 299 def explain analyze: false ensure_service! validate_analyze_option! analyze = ::Google::Cloud::Firestore::V1::ExplainOptions.new analyze: analyze responses_enum = service.run_aggregate_query @parent_path, @grpc, explain_options: AggregateQueryExplainResult.new responses_enum end |
#get {|snapshot| ... } ⇒ Enumerator<AggregateQuerySnapshot>
Retrieves aggregate snapshot for the query.
244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/google/cloud/firestore/aggregate_query.rb', line 244 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 |