我对mongodb相当陌生,可能有些奇怪的选项我错过了或什么,但我的减少函数有时返回嵌套对象,而不是对象中的值。
这里是我的函数
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 I 有时将嵌套对象作为求和值,而不是将其自身的值作为下面的输出捕获。
{" _id ":"STANL"、"价值":{"总和":"(对象对象]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();
}
HOLY!!!!!!!!!!!!!!!!!!…嗯…对不起,我错过了文档。
因为可以对同一个键多次调用reduce函数,所以下列属性必须为真:
返回对象的类型必须与map函数发出的值的类型相同,以确保以下操作为真:
减少(键,[C,减少(键,[A, B ]) ] ) == 减少(关键[C, A、B])
减小函数必须是幂等的。确保以下语句为真:
reduce(key, [reduce(key, valuesArray)]) == reduce(key, valuesArray)
valuesArray中元素的顺序不应该影响reduce函数的输出,因此下面的语句为真:
减少(关键[A, B]) = =减少(键,[B])