diff options
-rw-r--r-- | db/mr.cpp | 11 | ||||
-rw-r--r-- | jstests/mr3.js | 47 | ||||
-rw-r--r-- | shell/collection.js | 8 |
3 files changed, 65 insertions, 1 deletions
diff --git a/db/mr.cpp b/db/mr.cpp index 79042b23d41..521ef79fcd2 100644 --- a/db/mr.cpp +++ b/db/mr.cpp @@ -124,6 +124,13 @@ namespace mongo { finalizeCode = cmdObj["finalize"].ascode(); } + + if ( cmdObj["mapparams"].type() == Array ){ + mapparams = cmdObj["mapparams"].embeddedObjectUserCheck(); + } + else { + mapparams = BSONObj(); + } } { // query options @@ -174,6 +181,8 @@ namespace mongo { string mapCode; string reduceCode; string finalizeCode; + + BSONObj mapparams; // output tables string incLong; @@ -375,7 +384,7 @@ namespace mongo { if ( mr.verbose ) mt.reset(); state.scope->setThis( &o ); - if ( state.scope->invoke( state.map , BSONObj() , 0 , true ) ) + if ( state.scope->invoke( state.map , state.setup.mapparams , 0 , true ) ) throw UserException( (string)"map invoke failed: " + state.scope->getError() ); if ( mr.verbose ) mapTime += mt.micros(); diff --git a/jstests/mr3.js b/jstests/mr3.js new file mode 100644 index 00000000000..3ca5ef2b606 --- /dev/null +++ b/jstests/mr3.js @@ -0,0 +1,47 @@ + +t = db.mr3; +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( n , x ){ + x = x || 1; + this.tags.forEach( + function(z){ + for ( var i=0; i<x; i++ ) + emit( z , { count : n || 1 } ); + } + ); +}; + +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 ); +z = res.convertToSingleObject() + +assert.eq( 3 , z.keySet().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 , { mapparams : [ 2 , 2 ] } ); +z = res.convertToSingleObject() + +assert.eq( 3 , z.keySet().length , "B1" ); +assert.eq( 8 , z.a.count , "B2" ); +assert.eq( 12 , z.b.count , "B3" ); +assert.eq( 12 , z.c.count , "B4" ); + +res.drop(); + diff --git a/shell/collection.js b/shell/collection.js index 50a24b1de5e..03eb824d1d0 100644 --- a/shell/collection.js +++ b/shell/collection.js @@ -502,6 +502,14 @@ MapReduceResult.prototype.drop = function(){ return this._coll.drop(); } +/** +* just for debugging really +*/ +MapReduceResult.prototype.convertToSingleObject = function(){ + var z = {}; + this._coll.find().forEach( function(a){ z[a._id] = a.value; } ); + return z; +} /** * @param optional object of optional fields; |