0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 00:56:44 +01:00

optional params for map function SERVER-401

This commit is contained in:
Eliot Horowitz 2009-11-10 15:43:51 -05:00
parent c47308b1b4
commit 123cd0eb67
3 changed files with 65 additions and 1 deletions

View File

@ -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();

47
jstests/mr3.js Normal file
View File

@ -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();

View File

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