lib/mongoid/contextual/aggregable/mongo.rb in mongoid-3.1.7 vs lib/mongoid/contextual/aggregable/mongo.rb in mongoid-4.0.0.alpha1
- old
+ new
@@ -10,29 +10,25 @@
# @example Get all the aggregate values.
# aggregable.aggregates(:likes)
#
# @param [ String, Symbol ] field The field name.
#
- # @return [ Hash ] The aggregate values in the form:
+ # @return [ 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.
# {
# "count" => 2.0,
# "max" => 1000.0,
# "min" => 500.0,
# "sum" => 1500.0,
# "avg" => 750.0
# }
#
# @since 3.0.0
def aggregates(field)
- if query.count > 0
- result = collection.aggregate(pipeline(field)).to_a
- if result.empty?
- { "count" => query.count, "avg" => 0, "sum" => 0 }
- else
- result.first
- end
+ result = collection.aggregate(pipeline(field)).to_a
+ if result.empty?
+ { "count" => 0, "sum" => nil, "avg" => nil, "min" => nil, "max" => nil }
else
- { "count" => 0 }
+ result.first
end
end
# Get the average value of the provided field.
#