~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to jstests/group3.js

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
t = db.group2;
 
2
t.drop();
 
3
 
 
4
t.save({a: 1});
 
5
t.save({a: 2});
 
6
t.save({a: 3});
 
7
t.save({a: 4});
 
8
 
 
9
 
 
10
cmd = { initial: {count: 0, sum: 0},
 
11
        reduce: function(obj, prev) {
 
12
            prev.count++;
 
13
            prev.sum += obj.a;
 
14
        },
 
15
        finalize: function(obj) {
 
16
            if (obj.count){
 
17
                obj.avg = obj.sum / obj.count;
 
18
            }else{
 
19
                obj.avg = 0;
 
20
            }
 
21
        },
 
22
      };
 
23
 
 
24
result1 = t.group(cmd);
 
25
 
 
26
assert.eq(1, result1.length, "test1");
 
27
assert.eq(10, result1[0].sum, "test1");
 
28
assert.eq(4, result1[0].count, "test1");
 
29
assert.eq(2.5, result1[0].avg, "test1");
 
30
 
 
31
 
 
32
cmd['finalize'] = function(obj) {
 
33
    if (obj.count){
 
34
        return obj.sum / obj.count;
 
35
    }else{
 
36
        return 0;
 
37
    }
 
38
};
 
39
 
 
40
result2 = t.group(cmd);
 
41
 
 
42
assert.eq(1, result2.length, "test2");
 
43
assert.eq(2.5, result2[0], "test2");