File: mr_replaceIntoDB.js

package info (click to toggle)
mongodb 1%3A2.4.10-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,464 kB
  • sloc: cpp: 740,225; ansic: 152,098; sh: 13,820; python: 11,864; makefile: 1,012; perl: 922; pascal: 617; java: 452; lisp: 222; asm: 174
file content (45 lines) | stat: -rw-r--r-- 1,352 bytes parent folder | download | duplicates (4)
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.mr_replace;
t.drop();

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

outCollStr = "mr_replace_col";
outDbStr = "mr_db";

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;
}

print("Testing mr replace into other DB")
res = t.mapReduce( m , r , { out : { replace: outCollStr, db: outDbStr } } )
printjson( res );
expected = { "1" : 1 , "2" : 2 , "3" : 2 , "4" : 1 };
outDb = db.getMongo().getDB(outDbStr);
outColl = outDb[outCollStr];
str = tos( outColl.convertToSingleObject("value") )
print("Received result: " + str);
assert.eq( tos( expected ) , str , "A Received wrong result " + str );

print("checking result field");
assert.eq(res.result.collection, outCollStr, "B1 Wrong collection " + res.result.collection)
assert.eq(res.result.db, outDbStr, "B2 Wrong db " + res.result.db)

print("Replace again and check");
outColl.save({_id: "5", value : 1});
t.mapReduce( m , r , { out : { replace: outCollStr, db: outDbStr } } )
str = tos( outColl.convertToSingleObject("value") )
print("Received result: " + str);
assert.eq( tos( expected ) , str , "C1 Received wrong result " + str );