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
|
// @tags: [
// # Cannot implicitly shard accessed collections because of unsupported group operator on
// # sharded collection.
// assumes_unsharded_collection,
//
// # group requires javascript
// requires_scripting
// ]
t = db.jstests_group6;
t.drop();
for (i = 1; i <= 10; ++i) {
t.save({i: new NumberLong(i), y: 1});
}
assert.eq.automsg(
"55",
"t.group( {key:'y', reduce:function(doc,out){ out.i += doc.i; }, initial:{i:0} } )[ 0 ].i");
t.drop();
for (i = 1; i <= 10; ++i) {
if (i % 2 == 0) {
t.save({i: new NumberLong(i), y: 1});
} else {
t.save({i: i, y: 1});
}
}
assert.eq.automsg(
"55",
"t.group( {key:'y', reduce:function(doc,out){ out.i += doc.i; }, initial:{i:0} } )[ 0 ].i");
t.drop();
for (i = 1; i <= 10; ++i) {
if (i % 2 == 1) {
t.save({i: new NumberLong(i), y: 1});
} else {
t.save({i: i, y: 1});
}
}
assert.eq.automsg(
"55",
"t.group( {key:'y', reduce:function(doc,out){ out.i += doc.i; }, initial:{i:0} } )[ 0 ].i");
assert.eq.automsg(
"NumberLong(10)",
"t.group( {$reduce: function(doc, prev) { prev.count += 1; }, initial: {count: new NumberLong(0) }} )[ 0 ].count");
|