Method: Mongoid::Contextual::Aggregable::Mongo#max

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

#max(field = nil) ⇒ Float, Document

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.

Examples:

Get the max of a single field.

aggregable.max(:likes)

Get the document with the max value.

aggregable.max do |a, b|
  a.likes <=> b.likes
end

Parameters:

  • field (Symbol) (defaults to: nil)

    The field to max.

Returns:

  • (Float, Document)

    The max value or document with the max value.

Since:

  • 3.0.0



66
67
68
# File 'lib/mongoid/contextual/aggregable/mongo.rb', line 66

def max(field = nil)
  block_given? ? super() : aggregates(field)["max"]
end