Method: Mongoid::Contextual::Aggregable::Mongo#avg

Defined in:
lib/mongoid/contextual/aggregable/mongo.rb

#avg(field) ⇒ Float

Get the average value of the provided field.

Examples:

Get the average of a single field.

aggregable.avg(:likes)

Parameters:

  • field (Symbol)

    The field to average.

Returns:

  • (Float)

    The average.

Since:

  • 3.0.0



44
45
46
# File 'lib/mongoid/contextual/aggregable/mongo.rb', line 44

def avg(field)
  aggregates(field)["avg"]
end