-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaggregate-js-functions.js
More file actions
206 lines (192 loc) · 5.58 KB
/
aggregate-js-functions.js
File metadata and controls
206 lines (192 loc) · 5.58 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// $function operator - custom JavaScript functions in aggregation
// The JavaScript code is passed as a string in the "body" field
// Basic $function usage
db.players.aggregate([
{ $addFields: {
isFound: {
$function: {
body: "function(name) { return name.length > 5; }",
args: ["$name"],
lang: "js"
}
}
}}
])
// $function with multiple arguments
db.orders.aggregate([
{ $addFields: {
discount: {
$function: {
body: "function(price, quantity) { return price * quantity * 0.1; }",
args: ["$price", "$quantity"],
lang: "js"
}
}
}}
])
// $function with complex logic
db.events.aggregate([
{ $addFields: {
category: {
$function: {
body: "function(score) { if (score >= 90) return 'A'; else if (score >= 80) return 'B'; else if (score >= 70) return 'C'; else return 'F'; }",
args: ["$score"],
lang: "js"
}
}
}}
])
// $function with no arguments
db.items.aggregate([
{ $addFields: {
timestamp: {
$function: {
body: "function() { return new Date().toISOString(); }",
args: [],
lang: "js"
}
}
}}
])
// $function with array argument
db.data.aggregate([
{ $addFields: {
processed: {
$function: {
body: "function(arr) { return arr.map(x => x * 2); }",
args: ["$values"],
lang: "js"
}
}
}}
])
// $function with multiline string (escaped)
db.documents.aggregate([
{ $addFields: {
result: {
$function: {
body: "function(doc) {\n var sum = 0;\n for (var i = 0; i < doc.length; i++) {\n sum += doc[i];\n }\n return sum;\n}",
args: ["$numbers"],
lang: "js"
}
}
}}
])
// $accumulator operator - custom accumulator with JavaScript
db.sales.aggregate([
{ $group: {
_id: "$category",
customSum: {
$accumulator: {
init: "function() { return { sum: 0, count: 0 }; }",
accumulate: "function(state, value) { return { sum: state.sum + value, count: state.count + 1 }; }",
accumulateArgs: ["$amount"],
merge: "function(state1, state2) { return { sum: state1.sum + state2.sum, count: state1.count + state2.count }; }",
finalize: "function(state) { return state.sum / state.count; }",
lang: "js"
}
}
}}
])
// $accumulator with initArgs
db.transactions.aggregate([
{ $group: {
_id: "$accountId",
balance: {
$accumulator: {
init: "function(initial) { return initial; }",
initArgs: [0],
accumulate: "function(state, amount, type) { return type === 'credit' ? state + amount : state - amount; }",
accumulateArgs: ["$amount", "$type"],
merge: "function(s1, s2) { return s1 + s2; }",
lang: "js"
}
}
}}
])
// $accumulator without finalize
db.metrics.aggregate([
{ $group: {
_id: "$source",
values: {
$accumulator: {
init: "function() { return []; }",
accumulate: "function(state, val) { state.push(val); return state; }",
accumulateArgs: ["$value"],
merge: "function(s1, s2) { return s1.concat(s2); }",
lang: "js"
}
}
}}
])
// Combined $function and standard operators
db.products.aggregate([
{ $match: { inStock: true } },
{ $addFields: {
priceCategory: {
$function: {
body: "function(p) { return p < 10 ? 'cheap' : p < 100 ? 'moderate' : 'expensive'; }",
args: ["$price"],
lang: "js"
}
}
}},
{ $group: { _id: "$priceCategory", count: { $sum: 1 } } },
{ $sort: { count: -1 } }
])
// $function in $project stage
db.users.aggregate([
{ $project: {
name: 1,
maskedEmail: {
$function: {
body: "function(email) { var parts = email.split('@'); return parts[0].substring(0, 2) + '***@' + parts[1]; }",
args: ["$email"],
lang: "js"
}
}
}}
])
// $function with object argument
db.records.aggregate([
{ $addFields: {
serialized: {
$function: {
body: "function(obj) { return JSON.stringify(obj); }",
args: ["$metadata"],
lang: "js"
}
}
}}
])
// $function returning object
db.coordinates.aggregate([
{ $addFields: {
point: {
$function: {
body: "function(lat, lng) { return { type: 'Point', coordinates: [lng, lat] }; }",
args: ["$latitude", "$longitude"],
lang: "js"
}
}
}}
])
// Nested $function in $facet
db.analytics.aggregate([
{ $facet: {
processed: [
{ $addFields: {
normalized: {
$function: {
body: "function(v, min, max) { return (v - min) / (max - min); }",
args: ["$value", "$minValue", "$maxValue"],
lang: "js"
}
}
}}
],
summary: [
{ $group: { _id: null, total: { $sum: "$value" } } }
]
}}
])