生成MongoDB JavaScript的mapreduce函数存在问题
我试图通过MapReduce功能计算每条路线(route_I)的行程数(trip_I),但不起作用。他们还要求我使用finalize函数,但我还不知道如何使用。以下是数据:生成MongoDB JavaScript的mapreduce函数存在问题,javascript,mongodb,mapreduce,Javascript,Mongodb,Mapreduce,我试图通过MapReduce功能计算每条路线(route_I)的行程数(trip_I),但不起作用。他们还要求我使用finalize函数,但我还不知道如何使用。以下是数据: { "_id" : ObjectId("602018db89877fd06f61d2f2"), "from_stop_I" : 1908, "to_stop_I" : 1491, "dep_time"
{ "_id" : ObjectId("602018db89877fd06f61d2f2"), "from_stop_I" : 1908, "to_stop_I" : 1491, "dep_time" : ISODate("2016-09-05T07:40:00.000Z"), "arr_time" : ISODate("2016-09-05T07:41:00.000Z"), "route_type" : 3, "trip_I" : 18, "seq" : 37, "route_I" : 104}
{ "_id" : ObjectId("602018db89877fd06f61d2f3"), "from_stop_I" : 1491, "to_stop_I" : 1500, "dep_time" : ISODate("2016-09-05T07:41:00.000Z"), "arr_time" : ISODate("2016-09-05T07:44:00.000Z"), "route_type" : 3, "trip_I" : 18, "seq" : 38, "route_I" : 104}
{ "_id" : ObjectId("602018dc89877fd06f61d721"), "from_stop_I" : 1156, "to_stop_I" : 1158, "dep_time" : ISODate("2016-09-05T08:06:00.000Z"), "arr_time" : ISODate("2016-09-05T08:06:00.000Z"), "route_type" : 3, "trip_I" : 72, "seq" : 1, "route_I" : 104}
{ "_id" : ObjectId("602018dc89877fd06f61d722"), "from_stop_I" : 1158, "to_stop_I" : 1160, "dep_time" : ISODate("2016-09-05T08:06:00.000Z"), "arr_time" : ISODate("2016-09-05T08:07:00.000Z"), "route_type" : 3, "trip_I" : 72, "seq" : 2, "route_I" : 104}
{ "_id" : ObjectId("602018dc89877fd06f61d746"), "from_stop_I" : 1491, "to_stop_I" : 1500, "dep_time" : ISODate("2016-09-05T08:27:00.000Z"), "arr_time" : ISODate("2016-09-05T08:30:00.000Z"), "route_type" : 3, "trip_I" : 72, "seq" : 38, "route_I" : 104}
{ "_id" : ObjectId("6020193c89877fd06f639dec"), "from_stop_I" : 1156, "to_stop_I" : 1158, "dep_time" : ISODate("2016-09-05T23:20:00.000Z"), "arr_time" : ISODate("2016-09-05T23:20:00.000Z"), "route_type" : 3, "trip_I" : 6972, "seq" : 1, "route_I" : 104}
{ "_id" : ObjectId("6020193c89877fd06f639ded"), "from_stop_I" : 1158, "to_stop_I" : 1160, "dep_time" : ISODate("2016-09-05T23:20:00.000Z"), "arr_time" : ISODate("2016-09-05T23:21:00.000Z"), "route_type" : 3, "trip_I" : 6972, "seq" : 2, "route_I" : 104}
{ "_id" : ObjectId("6020193c89877fd06f639dee"), "from_stop_I" : 1160, "to_stop_I" : 1162, "dep_time" : ISODate("2016-09-05T23:21:00.000Z"), "arr_time" : ISODate("2016-09-05T23:21:00.000Z"), "route_type" : 3, "trip_I" : 6972, "seq" : 3, "route_I" : 104}
代码如下:
function mapFunction () {
var key = this.route_I;
var value = { totalTrips: this.trip_I,totalRegistros:1 };
emit( key, value );
};
function reduceFunction (key, trips) {
var reducedObject = { totalTrips: 0,totalRegistros:0 };
trips.forEach(function(value) {
reducedObject.totalTrips ++;
reducedObject.totalRegistros += value.totalRegistros;
})
;
return reducedObject;
};
db.routes.mapReduce(
mapFunction,
reduceFunction,
{
out: "routes_out"
}
)
路线104的输出应为3次行程。
谢谢你所说的“但不起作用”是什么意思?你得到了一个错误,没有回应,得到了错误的回应,还有别的什么?您是否尝试过使用聚合而不是mapreduce?它不会生成错误。它不起作用,因为它给我带来了不一致的价值观。我只需要用MapReduce就可以了。请给我一个提示。reduce函数完全忽略了trip_i值。