gpt4 book ai didi

node.js - mongodb mapreduce Nodejs减少返回嵌套对象而不是值

转载 作者:太空宇宙 更新时间:2023-11-03 23:41:38 25 4
gpt4 key购买 nike

我对 mongodb 相当陌生,可能我错过了一些奇怪的选项或其他东西,但我的reduce函数有时返回嵌套对象而不是对象中的值。

这是我的功能

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

使用reduce1的输出是完美的,但使用reduce2我有时将嵌套对象作为总和值而不是它本身的值作为下面的输出捕获。

{"_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}},

有人遇到过这种情况吗?

我的mapReduce函数命令如下...

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

最佳答案

天啊!!!!!!!!!!!!!!!!!!嗯...抱歉我错过了文档。

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 ] )

关于node.js - mongodb mapreduce Nodejs减少返回嵌套对象而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com