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
|
// @tags: [
// # mapReduce does not support afterClusterTime.
// does_not_support_causal_consistency,
// does_not_support_stepdowns,
// uses_map_reduce_with_temp_collections,
// ]
(function() {
"use strict";
const coll = db.mr_replace;
coll.drop();
assert.commandWorked(coll.insert([{a: [1, 2]}, {a: [2, 3]}, {a: [3, 4]}]));
const outCollStr = "mr_replace_col";
const outDbStr = "mr_db";
const outDb = db.getMongo().getDB(outDbStr);
const outColl = outDb[outCollStr];
const mapFn = function() {
for (i = 0; i < this.a.length; i++)
emit(this.a[i], 1);
};
const reduceFn = function(k, vs) {
return Array.sum(vs);
};
(function testReplace() {
let res = assert.commandWorked(
coll.mapReduce(mapFn, reduceFn, {out: {replace: outCollStr, db: outDbStr}}));
const expected =
[{_id: 1, value: 1}, {_id: 2, value: 2}, {_id: 3, value: 2}, {_id: 4, value: 1}];
let actual = outColl.find().sort({_id: 1}).toArray();
assert.eq(expected, actual);
assert.eq(res.result.collection, outCollStr, "Wrong collection " + res.result.collection);
assert.eq(res.result.db, outDbStr, "Wrong db " + res.result.db);
// Run the whole thing again and make sure it does the same thing.
assert.commandWorked(outColl.insert({_id: 5, value: 1}));
assert.commandWorked(
coll.mapReduce(mapFn, reduceFn, {out: {replace: outCollStr, db: outDbStr}}));
actual = outColl.find().sort({_id: 1}).toArray();
assert.eq(expected, actual);
}());
// TODO SERVER-42511 we should call 'testMerge()' and 'testReduce()' once we flip on the new
// implementation. Today these would fail in certain sharded passthroughs, as described in
// SERVER-44238.
function testMerge() {
outColl.drop();
assert.commandWorked(
outColl.insert([{_id: 1, value: "something else"}, {_id: 5, value: "existing"}]));
let res = assert.commandWorked(
coll.mapReduce(mapFn, reduceFn, {out: {merge: outCollStr, db: outDbStr}}));
const expected = [
{_id: 1, value: 1},
{_id: 2, value: 2},
{_id: 3, value: 2},
{_id: 4, value: 1},
{_id: 5, value: "existing"}
];
let actual = outColl.find().sort({_id: 1}).toArray();
assert.eq(expected, actual);
}
function testReduce() {
outColl.drop();
assert.commandWorked(outColl.insert([{_id: 1, value: 100}, {_id: 5, value: 0}]));
let res = assert.commandWorked(
coll.mapReduce(mapFn, reduceFn, {out: {reduce: outCollStr, db: outDbStr}}));
const expected = [
{_id: 1, value: 101},
{_id: 2, value: 2},
{_id: 3, value: 2},
{_id: 4, value: 1},
{_id: 5, value: 0}
];
let actual = outColl.find().sort({_id: 1}).toArray();
assert.eq(expected, actual);
}
}());
|