/
mongo.rb
141 lines (131 loc) · 4.5 KB
/
mongo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# frozen_string_literal: true
# rubocop:todo all
require "mongoid/contextual/aggregable"
module Mongoid
module Contextual
module Aggregable
# Contains behavior for aggregating values in Mongo.
module Mongo
# Get all the aggregate values for the provided field.
#
# @example Get all the aggregate values.
# aggregable.aggregates(:likes)
# # => {
# # "count" => 2.0,
# # "max" => 1000.0,
# # "min" => 500.0,
# # "sum" => 1500.0,
# # "avg" => 750.0
# # }
#
# @param [ String | Symbol ] field The field name.
#
# @return [ Hash ] A Hash containing the aggregate values.
# If no documents are found, then returned Hash will have
# count, sum of 0 and max, min, avg of nil.
def aggregates(field)
result = collection.aggregate(pipeline(field), session: _session).to_a
if result.empty?
Aggregable::EMPTY_RESULT.dup
else
result.first
end
end
# Get the average value of the provided field.
#
# @example Get the average of a single field.
# aggregable.avg(:likes)
#
# @param [ Symbol ] field The field to average.
#
# @return [ Float ] The average.
def avg(field)
aggregates(field)["avg"]
end
# 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.
#
# @example Get the max of a single field.
# aggregable.max(:likes)
#
# @example Get the document with the max value.
# aggregable.max do |a, b|
# a.likes <=> b.likes
# end
#
# @param [ Symbol ] field The field to max.
#
# @return [ Float | Document ] The max value or document with the max
# value.
def max(field = nil)
block_given? ? super() : aggregates(field)["max"]
end
# 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.
#
# @example Get the min of a single field.
# aggregable.min(:likes)
#
# @example Get the document with the min value.
# aggregable.min do |a, b|
# a.likes <=> b.likes
# end
#
# @param [ Symbol ] field The field to min.
#
# @return [ Float | Document ] The min value or document with the min
# value.
def min(field = nil)
block_given? ? super() : aggregates(field)["min"]
end
# Get the sum value of the provided field. If provided a block, will
# return the sum in accordance with Ruby's enumerable API.
#
# @example Get the sum of a single field.
# aggregable.sum(:likes)
#
# @example Get the sum for the provided block.
# aggregable.sum(&:likes)
#
# @param [ Symbol ] field The field to sum.
#
# @return [ Float ] The sum value.
def sum(field = nil)
block_given? ? super() : aggregates(field)["sum"] || 0
end
private
# Get the aggregation pipeline for provided field.
#
# @api private
#
# @example Get the pipeline.
# aggregable.pipeline(:likes)
#
# @param [ String | Symbol ] field The name of the field.
#
# @return [ Array ] The array of pipeline operators.
def pipeline(field)
db_field = "$#{database_field_name(field)}"
sort, skip, limit = criteria.options.values_at(:sort, :skip, :limit)
pipeline = []
pipeline << { "$match" => criteria.exists(field => true).selector }
pipeline << { "$sort" => sort } if sort && (skip || limit)
pipeline << { "$skip" => skip } if skip
pipeline << { "$limit" => limit } if limit
pipeline << {
"$group" => {
"_id" => field.to_s,
"count" => { "$sum" => 1 },
"max" => { "$max" => db_field },
"min" => { "$min" => db_field },
"sum" => { "$sum" => db_field },
"avg" => { "$avg" => db_field }
}
}
end
end
end
end
end