Module: Mongoid::Contextual::Aggregable::Mongo
- Included in:
- Mongo
- Defined in:
- lib/mongoid/contextual/aggregable/mongo.rb
Overview
Contains behavior for aggregating values in Mongo.
Instance Method Summary collapse
- #aggregates(field) ⇒ Hash deprecated Deprecated.
- #avg(field) ⇒ Float deprecated Deprecated.
- #max(field = nil) ⇒ Float | Document deprecated Deprecated.
- #min(field = nil) ⇒ Float | Document deprecated Deprecated.
- #sum(field = nil) ⇒ Float deprecated Deprecated.
Instance Method Details
#aggregates(field) ⇒ Hash
Deprecated.
Get all the aggregate values for the provided field.
30 31 32 33 34 35 36 37 |
# File 'lib/mongoid/contextual/aggregable/mongo.rb', line 30 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
Deprecated.
Get the average value of the provided field.
49 50 51 |
# File 'lib/mongoid/contextual/aggregable/mongo.rb', line 49 def avg(field) aggregates(field)["avg"] end |
#max(field = nil) ⇒ Float | Document
Deprecated.
Get the max value of the provided field. If provided a block, will return the Document with the greatest value for the field, in accordance with Ruby’s enumerable API.
71 72 73 |
# File 'lib/mongoid/contextual/aggregable/mongo.rb', line 71 def max(field = nil) block_given? ? super() : aggregates(field)["max"] end |
#min(field = nil) ⇒ Float | Document
Deprecated.
Get the min value of the provided field. If provided a block, will return the Document with the smallest value for the field, in accordance with Ruby’s enumerable API.
93 94 95 |
# File 'lib/mongoid/contextual/aggregable/mongo.rb', line 93 def min(field = nil) block_given? ? super() : aggregates(field)["min"] end |
#sum(field = nil) ⇒ Float
Deprecated.
Get the sum value of the provided field. If provided a block, will return the sum in accordance with Ruby’s enumerable API.
111 112 113 |
# File 'lib/mongoid/contextual/aggregable/mongo.rb', line 111 def sum(field = nil) block_given? ? super() : aggregates(field)["sum"] || 0 end |