From 123cd0eb6734f397ebf4236e0779d056243446d4 Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Tue, 10 Nov 2009 15:43:51 -0500 Subject: [PATCH] optional params for map function SERVER-401 --- db/mr.cpp | 11 ++++++++++- jstests/mr3.js | 47 +++++++++++++++++++++++++++++++++++++++++++++ shell/collection.js | 8 ++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 jstests/mr3.js 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