Method: Mongoid::Contextual::Aggregable::Memory#max

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

#max(field = nil) ⇒ Numeric | 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:

  • (Numeric | Document)

    The max value or document with the max value.



56
57
58
59
60
# File 'lib/mongoid/contextual/aggregable/memory.rb', line 56

def max(field = nil)
  return super() if block_given?

  aggregate_by(field, :max)
end