Module: Mongoid::Contextual::Aggregable::Mongo
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | lib/mongoid/contextual/aggregable/mongo.rb |
Overview
Contains behavior for aggregating values in Mongo
.
Instance Method Summary
-
#aggregates(field) ⇒ Hash
Get all the aggregate values for the provided field.
-
#avg(field) ⇒ Float
Get the average value of the provided field.
-
#max(field = nil) ⇒ Float | Document
Get the max value of the provided field.
-
#min(field = nil) ⇒ Float | Document
Get the min value of the provided field.
-
#sum(field = nil) ⇒ Float
Get the sum value of the provided field.
-
#pipeline(field) ⇒ Array
private
Internal use only
Internal use only
Get the aggregation pipeline for provided field.
Instance Method Details
#aggregates(field) ⇒ Hash
Get all the aggregate values for the provided field.
# File 'lib/mongoid/contextual/aggregable/mongo.rb', line 29
def aggregates(field) result = collection.aggregate(pipeline(field), session: _session).to_a if result.empty? Aggregable::EMPTY_RESULT.dup else result.first end end
#avg(field) ⇒ Float
Get the average value of the provided field.
# File 'lib/mongoid/contextual/aggregable/mongo.rb', line 46
def avg(field) aggregates(field)["avg"] end
#max(field = nil) ⇒ Float | Document
Get the max value of the provided field. If provided a block, will return the ::Mongoid::Document
with the greatest value for the field, in accordance with Ruby’s enumerable API.
# File 'lib/mongoid/contextual/aggregable/mongo.rb', line 66
def max(field = nil) block_given? ? super() : aggregates(field)["max"] end
#min(field = nil) ⇒ Float | Document
Get the min value of the provided field. If provided a block, will return the ::Mongoid::Document
with the smallest value for the field, in accordance with Ruby’s enumerable API.
# File 'lib/mongoid/contextual/aggregable/mongo.rb', line 86
def min(field = nil) block_given? ? super() : aggregates(field)["min"] end
#pipeline(field) ⇒ Array (private)
Get the aggregation pipeline for provided field.
# File 'lib/mongoid/contextual/aggregable/mongo.rb', line 121
def pipeline(field) db_field = "$#{database_field_name(field)}" sort, skip, limit = criteria. .values_at(:sort, :skip, :limit) pipeline = [] pipeline << { "$match" => criteria.exists(field => true).selector } pipeline << { "$sort" => sort } if sort && (skip || limit) pipeline << { "$skip" => skip } if skip pipeline << { "$limit" => limit } if limit pipeline << { "$group" => { "_id" => field.to_s, "count" => { "$sum" => 1 }, "max" => { "$max" => db_field }, "min" => { "$min" => db_field }, "sum" => { "$sum" => db_field }, "avg" => { "$avg" => db_field } } } end
#sum(field = nil) ⇒ Float
Get the sum value of the provided field. If provided a block, will return the sum in accordance with Ruby’s enumerable API.
# File 'lib/mongoid/contextual/aggregable/mongo.rb', line 103
def sum(field = nil) return super(field || 0) if block_given? aggregates(field)["sum"] || 0 end