-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathaccumulator.yaml
168 lines (168 loc) · 6.67 KB
/
accumulator.yaml
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# $schema: ../schema.json
name: $accumulator
link: https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator/
type:
- accumulator
encode: object
description: |
Defines a custom accumulator function.
New in MongoDB 4.4.
arguments:
-
name: init
type:
- javascript
description: |
Function used to initialize the state. The init function receives its arguments from the initArgs array expression. You can specify the function definition as either BSON type Code or String.
-
name: initArgs
type:
- resolvesToArray
optional: true
description: |
Arguments passed to the init function.
-
name: accumulate
type:
- javascript
description: |
Function used to accumulate documents. The accumulate function receives its arguments from the current state and accumulateArgs array expression. The result of the accumulate function becomes the new state. You can specify the function definition as either BSON type Code or String.
-
name: accumulateArgs
type:
- resolvesToArray
description: |
Arguments passed to the accumulate function. You can use accumulateArgs to specify what field value(s) to pass to the accumulate function.
-
name: merge
type:
- javascript
description: |
Function used to merge two internal states. merge must be either a String or Code BSON type. merge returns the combined result of the two merged states. For information on when the merge function is called, see Merge Two States with $merge.
-
name: finalize
type:
- javascript
optional: true
description: |
Function used to update the result of the accumulation.
-
name: lang
type:
- string
description: |
The language used in the $accumulator code.
tests:
-
name: Use $accumulator to Implement the $avg Operator
link: https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator/#use--accumulator-to-implement-the--avg-operator
pipeline:
-
$group:
_id: $author
avgCopies:
$accumulator:
init:
$code: |-
function() {
return { count: 0, sum: 0 }
}
accumulate:
$code: |-
function(state, numCopies) {
return { count: state.count + 1, sum: state.sum + numCopies }
}
accumulateArgs:
- $copies
merge:
$code: |-
function(state1, state2) {
return {
count: state1.count + state2.count,
sum: state1.sum + state2.sum
}
}
finalize:
$code: |-
function(state) {
return (state.sum / state.count)
}
lang: js
schema:
books:
_id:
types:
-
bsonType: Number
title:
types:
-
bsonType: String
author:
types:
-
bsonType: String
copies:
types:
-
bsonType: Number
-
name: Use initArgs to Vary the Initial State by Group
link: https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator/#use-initargs-to-vary-the-initial-state-by-group
pipeline:
-
$group:
_id:
city: $city
restaurants:
$accumulator:
init:
$code: |-
function(city, userProfileCity) {
return { max: city === userProfileCity ? 3 : 1, restaurants: [] }
}
initArgs:
- $city
- Bettles
accumulate:
$code: |-
function(state, restaurantName) {
if (state.restaurants.length < state.max) {
state.restaurants.push(restaurantName);
}
return state;
}
accumulateArgs:
- $name
merge:
$code: |-
function(state1, state2) {
return {
max: state1.max,
restaurants: state1.restaurants.concat(state2.restaurants).slice(0, state1.max)
}
}
finalize:
$code: |-
function(state) {
return state.restaurants
}
lang: js
schema:
restaurants:
_id:
types:
-
bsonType: Number
name:
types:
-
bsonType: String
city:
types:
-
bsonType: String
cuisine:
types:
-
bsonType: String