File: group3.js

package info (click to toggle)
mongodb 1%3A2.0.6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,204 kB
  • sloc: cpp: 109,783; ansic: 101,073; python: 2,287; perl: 395; makefile: 370; sh: 242; asm: 46
file content (43 lines) | stat: -rw-r--r-- 845 bytes parent folder | download | duplicates (5)
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
t = db.group3;
t.drop();

t.save({a: 1});
t.save({a: 2});
t.save({a: 3});
t.save({a: 4});


cmd = { initial: {count: 0, sum: 0},
        reduce: function(obj, prev) {
            prev.count++;
            prev.sum += obj.a;
        },
        finalize: function(obj) {
            if (obj.count){
                obj.avg = obj.sum / obj.count;
            }else{
                obj.avg = 0;
            }
        },
      };

result1 = t.group(cmd);

assert.eq(1, result1.length, "test1");
assert.eq(10, result1[0].sum, "test1");
assert.eq(4, result1[0].count, "test1");
assert.eq(2.5, result1[0].avg, "test1");


cmd['finalize'] = function(obj) {
    if (obj.count){
        return obj.sum / obj.count;
    }else{
        return 0;
    }
};

result2 = t.group(cmd);

assert.eq(1, result2.length, "test2");
assert.eq(2.5, result2[0], "test2");