Module: Mongoid::Contextual::Aggregable::Mongo

Included in:
Mongo
Defined in:
build/mongoid-7.3/lib/mongoid/contextual/aggregable/mongo.rb

Overview

Contains behavior for aggregating values in Mongo.

Instance Method Summary collapse

Instance Method Details

#aggregates(field) ⇒ Hash

Get all the aggregate values for the provided field.

Examples:

Get all the aggregate values.

aggregable.aggregates(:likes)
# => {
#   "count" => 2.0,
#   "max" => 1000.0,
#   "min" => 500.0,
#   "sum" => 1500.0,
#   "avg" => 750.0
# }

Parameters:

  • field (String, Symbol)

    The field name.

Returns:

  • (Hash)

    count is a number of documents with the provided field. If there’re none, then count is 0 and max, min, sum, avg are nil.

Since:

  • 3.0.0



29
30
31
32
33
34
35
36
# File 'build/mongoid-7.3/lib/mongoid/contextual/aggregable/mongo.rb', line 29

def aggregates(field)
  result = collection.find.aggregate(pipeline(field), session: _session).to_a
  if result.empty?
    { "count" => 0, "sum" => nil, "avg" => nil, "min" => nil, "max" => nil }
  else
    result.first
  end
end

#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



48
49
50
# File 'build/mongoid-7.3/lib/mongoid/contextual/aggregable/mongo.rb', line 48

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

#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



70
71
72
# File 'build/mongoid-7.3/lib/mongoid/contextual/aggregable/mongo.rb', line 70

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

#min(field = nil) ⇒ Float, Document

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.

Examples:

Get the min of a single field.

aggregable.min(:likes)

Get the document with the min value.

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

Parameters:

  • field (Symbol) (defaults to: nil)

    The field to min.

Returns:

  • (Float, Document)

    The min value or document with the min value.

Since:

  • 3.0.0



92
93
94
# File 'build/mongoid-7.3/lib/mongoid/contextual/aggregable/mongo.rb', line 92

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

#sum(field = nil) ⇒ Float

Get the sum value of the provided field. If provided a block, will return the sum in accordance with Ruby’s enumerable API.

Examples:

Get the sum of a single field.

aggregable.sum(:likes)

Get the sum for the provided block.

aggregable.sum(&:likes)

Parameters:

  • field (Symbol) (defaults to: nil)

    The field to sum.

Returns:

  • (Float)

    The sum value.

Since:

  • 3.0.0



110
111
112
# File 'build/mongoid-7.3/lib/mongoid/contextual/aggregable/mongo.rb', line 110

def sum(field = nil)
  block_given? ? super() : aggregates(field)["sum"] || 0
end