summaryrefslogtreecommitdiff
path: root/jstests/core/mr_merge2.js
blob: 8bdc2a90216ff8e6b07d9178a1948eef93a89917 (plain)
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
// Cannot implicitly shard accessed collections because of following errmsg: Cannot output to a
// non-sharded collection because sharded collection exists already.
// @tags: [assumes_unsharded_collection, does_not_support_stepdowns]

t = db.mr_merge2;
t.drop();

t.insert({a: [1, 2]});
t.insert({a: [2, 3]});
t.insert({a: [3, 4]});

outName = "mr_merge2_out";
out = db[outName];
out.drop();

m = function() {
    for (i = 0; i < this.a.length; i++)
        emit(this.a[i], 1);
};
r = function(k, vs) {
    return Array.sum(vs);
};

function tos(o) {
    var s = "";
    for (var i = 0; i < 100; i++) {
        if (o[i])
            s += i + "_" + o[i] + "|";
    }
    return s;
}

outOptions = {
    out: {merge: outName}
};

res = t.mapReduce(m, r, outOptions);
expected = {
    "1": 1,
    "2": 2,
    "3": 2,
    "4": 1
};
assert.eq(tos(expected), tos(res.convertToSingleObject()), "A");

t.insert({a: [4, 5]});
res = t.mapReduce(m, r, outOptions);
expected["4"]++;
expected["5"] = 1;
assert.eq(tos(expected), tos(res.convertToSingleObject()), "B");