mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
c0fa671318
you have to specify out SERVER-1837
52 lines
1.1 KiB
JavaScript
52 lines
1.1 KiB
JavaScript
|
|
t = db.mr_merge;
|
|
t.drop();
|
|
|
|
t.insert( { a : [ 1 , 2 ] } )
|
|
t.insert( { a : [ 2 , 3 ] } )
|
|
t.insert( { a : [ 3 , 4 ] } )
|
|
|
|
outName = "mr_merge_out";
|
|
out = db[outName];
|
|
out.drop();
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
res = t.mapReduce( m , r , { out : outName } )
|
|
|
|
|
|
expected = { "1" : 1 , "2" : 2 , "3" : 2 , "4" : 1 }
|
|
assert.eq( tos( expected ) , tos( res.convertToSingleObject() ) , "A" );
|
|
|
|
t.insert( { a : [ 4 , 5 ] } )
|
|
out.insert( { _id : 10 , value : "5" } )
|
|
res = t.mapReduce( m , r , { out : outName } )
|
|
|
|
expected["4"]++;
|
|
expected["5"] = 1
|
|
assert.eq( tos( expected ) , tos( res.convertToSingleObject() ) , "B" );
|
|
|
|
t.insert( { a : [ 5 , 6 ] } )
|
|
out.insert( { _id : 10 , value : "5" } )
|
|
res = t.mapReduce( m , r , { out : { merge : outName } } )
|
|
|
|
expected["5"]++;
|
|
expected["10"] = 5
|
|
expected["6"] = 1
|
|
|
|
assert.eq( tos( expected ) , tos( res.convertToSingleObject() ) , "C" );
|
|
|
|
|
|
|