slimbikr
slimbikr

Reputation: 116

mongodb mapreduce nodejs reduce returning nested objects and not values

I'm fairly new to mongodb and probably some weird option i missed or something but my reduce function sometimes returns nested objects rather than a value in an object.

here are my functions

var map = function () {
    emit( this.symbol, this.value);
};

var reduce1 = function (key, values) {
    var sum =  Array.sum(values);
    return sum;
}

var reduce2 = function (key, values) {
    var sum =  Array.sum(values);
    return { sum : sum };
}

The output is perfect using reduce1 but with reduce2 I sometimes get nested objects as the sum value rather than the value it self as the output capture below.

{"_id":"STANL","value":{"sum":"[object Object]0.99849857369507570.75736597102254950.88874422199529520.96082746522239491.14145582711759231.48978478848374980.90384569740327051.19704634790183520.88402968789822790.89298097198428821.29725185407638041.14841485460061481.09300962993201020.8907340112179960.96429537804144251.0227290260333571.04124365277301761.20668054319056450.99881383888825951.09970609158237950.75072708303315081.33870315588283641.3228261565037411.1991018787824881.24721640669130341.34837757946851490.80527964298505161.03347018232696650.68659975004394341.0242879181930671.35051628955444110.88796458999640491.46867330372019441.11862899303530841.09569628690573161.22322404865043240.71685393857070531.03446518877196870.92802251988624730.80307471198310821.23238958977232230.95043282711668911.204766056391251.06854713071140050.87119931348936911.15116766755811570.99578368720424220.95643964126054120.9162666117944730.97027742521994781.23653239202411761.03936602096239541.03923426775021670.89428017311011421.22995444926102810.89276806244443620.83134315618311790.88394094177891110.85125414239730950.82012388288529131.11068031460715581.39161712446953860.86701942697230821.1038005755665747"}},

{"_id":"TM","value":{"sum":87.80667518618023}},

{"_id":"TRI","value":{"sum":82.27787495206451}},

{"_id":"UPS","value":{"sum":91.25156384875487}},

Anyone encountered this before??

My mapReduce function command is like the following...

var Coll = db.collection('Moves');
try {
    Coll.mapReduce(map, reduce, 
        { 
            out : { inline: 1 },
            query : { SOME QUERY }
        }, 
        function(err, collection) {
            console.log(JSON.stringify(collection));
            db.close();
            return;
        });
}
catch(e) {
    console.log("error:"+e);
    db.close();
}

Upvotes: 1

Views: 971

Answers (1)

slimbikr
slimbikr

Reputation: 116

HOLY!!!!!!!!!!!!!!!!!!... hmm... sorry I missed the documentation.

Because it is possible to invoke the reduce function more than once for the same key, the following properties need to be true:

the type of the return object must be identical to the type of the value emitted by the map function to ensure that the following operations is true:

reduce(key, [ C, reduce(key, [ A, B ]) ] ) == reduce( key, [ C, A, B ] )

the reduce function must be idempotent. Ensure that the following statement is true:

reduce( key, [ reduce(key, valuesArray) ] ) == reduce( key, valuesArray )

the order of the elements in the valuesArray should not affect the output of the reduce function, so that the following statement is true:

reduce( key, [ A, B ] ) == reduce( key, [ B, A ] )

Upvotes: 2

Related Questions