Method: MongoDoc::Contexts::Mongo#avg
- Defined in:
- lib/mongo_doc/contexts/mongo.rb
#avg(field) ⇒ Object
Get the average value for the supplied field.
This will take the internally built selector and options and pass them on to the Ruby driver’s group() method on the collection. The collection itself will be retrieved from the class provided, and once the query has returned it will provided a grouping of keys with averages.
Example:
context.avg(:age)
Returns:
A numeric value that is the average.
43 44 45 46 |
# File 'lib/mongo_doc/contexts/mongo.rb', line 43 def avg(field) total = sum(field) total ? (total / count) : nil end |