Problem on ranking and map/reduce

26 views
Skip to first unread message

Enrico Pavesi

unread,
Apr 26, 2012, 8:33:56 AM4/26/12
to mongod...@googlegroups.com

I have a collection with a set and a count:

 function Map() {
emit(   this._id.P1,
                    {PROD : this._id.P2, VALORE : this.value, RANK1 : "", MAX1 : 0,RANK2 : "", MAX2 : 0,RANK3 : "", MAX3 : 0} );
emit(   this._id.P2,
                    {PROD : this._id.P1, VALORE : this.value, RANK1 : "", MAX1 : 0,RANK2 : "", MAX2 : 0,RANK3 : "", MAX3 : 0} );
},

function Reduce(key, values) {

var MASSIMO = values[0] ;

values.forEach(function(val) {
if(val.VALORE > MASSIMO.MAX1)
{
   MASSIMO.RANK3 = MASSIMO.RANK2;
MASSIMO.MAX3 = MASSIMO.MAX2;
   MASSIMO.RANK2 = MASSIMO.RANK1;
MASSIMO.MAX2 = MASSIMO.MAX1;
MASSIMO.RANK1 = val.PROD;
MASSIMO.MAX1 = val.VALORE;
}
else if(val.VALORE > MASSIMO.MAX2)
{
   MASSIMO.RANK3 = MASSIMO.RANK2;
MASSIMO.MAX3 = MASSIMO.MAX2;
MASSIMO.RANK2 = val.PROD;
MASSIMO.MAX2 = val.VALORE;
}
else if(val.VALORE > MASSIMO.MAX3)
{
MASSIMO.RANK3 = val.PROD;
MASSIMO.MAX3 = val.VALORE;
}

});
 
return  MASSIMO;
}

Seems that the reduce function is called more times and not keeping the old values, and so some results are ok others completely incorrect
Probably i'm missing something on how map reduce work.

Any hints?

Thanks

Enrico

Kristina Chodorow

unread,
Apr 26, 2012, 10:14:17 AM4/26/12
to mongodb-user
I'm not sure exactly what you're trying to do, but yes, MapReduce is
designed to call reduce() multiple times. See
http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-ReduceFunction:

"Because the reduce function might be invoked more than once for the
same key, the structure of the object returned by the reduce function
must be identical to the structure of the map function's emitted
value."
Reply all
Reply to author
Forward
0 new messages