summaryrefslogtreecommitdiff
path: root/jstests/mr4.js
blob: b14cdfef88e22d5b51027cc517f6f29dfc31b74d (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

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

t.save( { x : 1 , tags : [ "a" , "b" ] } );
t.save( { x : 2 , tags : [ "b" , "c" ] } );
t.save( { x : 3 , tags : [ "c" , "a" ] } );
t.save( { x : 4 , tags : [ "b" , "c" ] } );

m = function(){
    this.tags.forEach(
        function(z){
            emit( z , { count : xx } );
        }
    );
};

r = function( key , values ){
    var total = 0;
    for ( var i=0; i<values.length; i++ ){
        total += values[i].count;
    }
    return { count : total };
};

res = t.mapReduce( m , r , { scope : { xx : 1 } } );
z = res.convertToSingleObject()

assert.eq( 3 , Object.keySet( z ).length , "A1" );
assert.eq( 2 , z.a.count , "A2" );
assert.eq( 3 , z.b.count , "A3" );
assert.eq( 3 , z.c.count , "A4" );

res.drop();


res = t.mapReduce( m , r , { scope : { xx : 2 } } );
z = res.convertToSingleObject()

assert.eq( 3 , Object.keySet( z ).length , "A1" );
assert.eq( 4 , z.a.count , "A2" );
assert.eq( 6 , z.b.count , "A3" );
assert.eq( 6 , z.c.count , "A4" );

res.drop();