Class: SumBdy

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_scope.rb

Instance Method Summary collapse

Instance Method Details

#sum_by(ops) ⇒ Object



11
12
13
# File 'lib/mongo_scope.rb', line 11

def sum_by(ops)
  sum_by_raw(ops).inject({}) { |h,a| k = ops[:key]; h.merge(a[k] => a['count'])}
end

#sum_by_raw(ops) ⇒ Object



6
7
8
9
10
# File 'lib/mongo_scope.rb', line 6

def sum_by_raw(ops)
  reduce_function = "function (obj, prev) { prev.count += (obj.#{ops[:sum_field]} ? obj.#{ops[:sum_field]} : 0); }"
  code = Mongo::Code.new(reduce_function)
  group([ops[:key]].flatten, ops[:filter]||{}, {"count" => 0},code)
end