node.js - mongodb mapreduce nodejs reduce returning nested objects and not values -
i'm new mongodb , weird option missed or reduce function returns nested objects rather value in object.
here 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 perfect using reduce1 reduce2 sometimes nested objects sum value rather value self 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 before??
my mapreduce function command following...
var coll = db.collection('moves'); try { coll.mapreduce(map, reduce, { out : { inline: 1 }, query : { query } }, function(err, collection) { console.log(json.stringify(collection)); db.close(); return; }); } catch(e) { console.log("error:"+e); db.close(); }
holy!!!!!!!!!!!!!!!!!!... hmm... sorry missed documentation.
because possible invoke reduce function more once same key, following properties need true:
the type of return object must identical type of value emitted map function ensure following operations true:
reduce(key, [ c, reduce(key, [ a, b ]) ] ) == reduce( key, [ c, a, b ] )
the reduce function must idempotent. ensure following statement true:
reduce( key, [ reduce(key, valuesarray) ] ) == reduce( key, valuesarray )
the order of elements in valuesarray should not affect output of reduce function, following statement true:
reduce( key, [ a, b ] ) == reduce( key, [ b, ] )
Comments
Post a Comment