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
|
// @tags: [
// # mapReduce does not support afterClusterTime.
// does_not_support_causal_consistency,
// does_not_support_stepdowns,
// ]
// See SERVER-9448
// Test argument and receiver (aka 'this') objects and their children can be mutated
// in Map, Reduce and Finalize functions
(function() {
"use strict";
load("jstests/aggregation/extras/utils.js"); // For assertArrayEq.
const collection = db.mrMutableReceiver;
collection.drop();
collection.insert({a: 1});
const map = function() {
// set property on receiver
this.feed = {beef: 1};
// modify property on receiever
this.a = {cake: 1};
emit(this._id, this.feed);
emit(this._id, this.a);
};
const reduce = function(key, values) {
// set property on receiver
this.feed = {beat: 1};
// set property on key arg
key.fed = {mochi: 1};
// push properties onto values array arg
values.push(this.feed);
values.push(key.fed);
// modify each value in the (modified) array arg
values.forEach(function(val) {
val.mod = 1;
});
return {food: values};
};
const finalize = function(key, values) {
// set property on receiver
this.feed = {ice: 1};
// set property on key arg
key.fed = {cream: 1};
// push properties onto values array arg
printjson(values);
values.food.push(this.feed);
values.food.push(key.fed);
// modify each value in the (modified) array arg
values.food.forEach(function(val) {
val.mod = 1;
});
return values;
};
const cmdResult = collection.mapReduce(map, reduce, {finalize: finalize, out: {inline: 1}});
assertArrayEq(cmdResult.results[0].value.food, [
{"cake": 1, "mod": 1},
{"beef": 1, "mod": 1},
{"beat": 1, "mod": 1},
{"mochi": 1, "mod": 1},
{"ice": 1, "mod": 1},
{"cream": 1, "mod": 1}
]);
}());
|