我需要使用临时变量将值临时存储在reduce函数中。在我的输出日志中,结果表明reduce函数运行的次数是0.但是,当我执行其他类型的计算时,它会运行。减少方法不在mongodb mapreduce中运行
当我执行代码时,reduce函数不能访问对象。如果它有任何帮助,我使用mongojs库作为nodejs。
var map = function() {
emit(this._id,
{
d1: this.day_chan1,
d2: this.day_chan2,
d3: this.day_chan3
});
};
var reduce = function(key, values) {
var data = {};
var curr = values[0];
data.d1 = curr.d1 - prev.d1;
data.d2 = curr.d2 - prev.d2;
data.d3 = curr.d3 - prev.d3;
prev = curr;
return { timestamp: key , data: data };
};
var prev = { d1: 0, d2: 0, d3: 0 };
this
.db
.mapReduce(
map,
reduce,
{
scope: { prev : prev },
sort: { timestamp: 1 },
query: {},
out: 'temp'
}
,
function (err, data) {
if(err) callback(err);
else callback(data);
});
这是数据的样本,我处理
{
"_id" : ObjectId("5650845744aeea3dba90326d"),
"timestamp" : "2015-09-21 05:02:52",
"curr_property" : NumberInt(1818),
"curr_property_cost" : NumberInt(21),
"day_property" : NumberInt(30),
"day_property_cost" : NumberInt(18),
"curr_solar_generating" : NumberInt(676),
"curr_solar_export" : NumberInt(0),
"day_solar_generated" : NumberInt(11),
"day_solar_export" : NumberInt(0),
"curr_chan1" : NumberInt(676),
"curr_chan2" : NumberInt(676),
"curr_chan3" : NumberInt(466),
"day_chan1" : NumberInt(11),
"day_chan2" : NumberInt(11),
"day_chan3" : NumberInt(7),
"gatewayId" : 23.0
}
{
"_id" : ObjectId("5650845744aeea3dba90326e"),
"timestamp" : "2015-09-21 05:03:52",
"curr_property" : NumberInt(1818),
"curr_property_cost" : NumberInt(21),
"day_property" : NumberInt(60),
"day_property_cost" : NumberInt(18),
"curr_solar_generating" : NumberInt(676),
"curr_solar_export" : NumberInt(0),
"day_solar_generated" : NumberInt(22),
"day_solar_export" : NumberInt(0),
"curr_chan1" : NumberInt(676),
"curr_chan2" : NumberInt(676),
"curr_chan3" : NumberInt(466),
"day_chan1" : NumberInt(22),
"day_chan2" : NumberInt(22),
"day_chan3" : NumberInt(15),
"gatewayId" : 23.0
}
{
"_id" : ObjectId("5650845744aeea3dba90326f"),
"timestamp" : "2015-09-21 05:04:52",
"curr_property" : NumberInt(1818),
"curr_property_cost" : NumberInt(21),
"day_property" : NumberInt(91),
"day_property_cost" : NumberInt(19),
"curr_solar_generating" : NumberInt(676),
"curr_solar_export" : NumberInt(0),
"day_solar_generated" : NumberInt(33),
"day_solar_export" : NumberInt(0),
"curr_chan1" : NumberInt(676),
"curr_chan2" : NumberInt(676),
"curr_chan3" : NumberInt(466),
"day_chan1" : NumberInt(33),
"day_chan2" : NumberInt(33),
"day_chan3" : NumberInt(23),
"gatewayId" : 23.0
}
,这是我的日志输出
{
"result" : "temp",
"timeMillis" : 3362,
"counts" : {
"input" : 39781,
"emit" : 39781,
"reduce" : 0,
"output" : 39781
},
"ok" : 1
}
这是临时收集的外观在手术后
{
"_id" : ObjectId("5650845744aeea3dba90326d"),
"value" : {
"d1" : 11.0,
"d2" : 11.0,
"d3" : 7.0
}
}
{
"_id" : ObjectId("5650845744aeea3dba90326e"),
"value" : {
"d1" : 22.0,
"d2" : 22.0,
"d3" : 15.0
}
}
{
"_id" : ObjectId("5650845744aeea3dba90326f"),
"value" : {
"d1" : 33.0,
"d2" : 33.0,
"d3" : 23.0
}
}
她e是我所希望得到的
{
"_id" : ObjectId("5650845744aeea3dba90326d"),
"value" : {
"d1" : 0,
"d2" : 0,
"d3" : 0
}
}
{
"_id" : ObjectId("5650845744aeea3dba90326e"),
"value" : {
"d1" : 11.0,
"d2" : 11.0,
"d3" : 9.0
}
}
{
"_id" : ObjectId("5650845744aeea3dba90326f"),
"value" : {
"d1" : 11.0,
"d2" : 11.0,
"d3" : 18.0
}
}
我在这一块上敲了敲头,非常感谢一些帮助。
UPDATE:
代码当前服务的方法在内部运行mongojs。这在mongo控制台中是相当的。
db.testdb.mapReduce(map,
reduce,
{
scope: { prev : prev },
sort: { timestamp: 1 },
query: {},
out: 'temp'
});
map和reduce方法的声明与顶部的声明相同。
你能编辑你的问题,向我们展示以下三个元素,可能会增加你得到正确答案的机会;一些示例文档,实际输出和您的预期结果? – chridam
@chridam刚更新了它。 – Bazinga777
你可以放置prev高于reduce函数吗? – Ravenous