You need to enable JavaScript to run this app. MongoDB JavaScript MapReduce统计线路行程数及finalize应用问题
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB JavaScript MapReduce统计线路行程数及finalize应用问题

MongoDB MapReduce 统计独立线路行程数实现

核心需求

统计每条线路(route_I)对应的独立行程(trip_I)数量,业务强制要求使用finalize函数,针对route_I:104的线路,预期统计结果为3条独立行程。

测试数据集

// 共8条记录,对应3个不同的trip_I:18、72、6972
{ "_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 }

原有代码问题排查

  • 未实现去重逻辑:直接对所有emit的记录做计数,同一行程下的多条站点记录会被重复统计,无法得到独立行程数。
  • 违反MapReduce执行约束:MongoDB要求reduce函数的返回值结构必须与map函数emit的value结构完全一致,当前代码中map输出的totalTrips字段存储的是具体trip_I数值,而reduce返回的totalTrips是自增计数,结构不匹配会导致多次reduce调用时逻辑错乱。
  • 未按业务要求实现finalize函数。

可运行实现方案

核心逻辑:map阶段将每条记录的trip_I存入数组做初始value;reduce阶段合并同key下的所有trip数组并做去重,保证返回结构与map完全一致;finalize阶段计算去重后的数组长度,得到最终独立行程数。

// map函数:按线路分组,携带单条记录的trip_I值
function mapFunction() {
    var key = this.route_I;
    // 用数组存储trip_I,保证后续reduce的返回结构与map完全匹配
    var value = { tripSet: [this.trip_I] };
    emit(key, value);
};

// reduce函数:合并同key下的所有trip值,完成去重
function reduceFunction(key, tripValues) {
    var mergedTrips = [];
    tripValues.forEach(function(item) {
        mergedTrips = mergedTrips.concat(item.tripSet);
    });
    // 对合并后的数组去重
    var uniqueTrips = Array.from(new Set(mergedTrips));
    return { tripSet: uniqueTrips };
};

// finalize函数:满足业务要求,在reduce完成后计算最终统计值
function finalizeFunction(key, reducedValue) {
    return {
        route_I: key,
        uniqueTripCount: reducedValue.tripSet.length,
        tripList: reducedValue.tripSet
    };
}

// 执行MapReduce任务
db.routes.mapReduce(
    mapFunction,
    reduceFunction,
    {
        out: "routes_out",
        finalize: finalizeFunction
    }
)

运行结果验证

执行上述代码后查询输出集合:

db.routes_out.find({_id: 104})

返回结果完全符合预期:

{
    "_id": 104,
    "value": {
        "route_I": 104,
        "uniqueTripCount": 3,
        "tripList": [18, 72, 6972]
    }
}

内容的提问来源于stack exchange,提问作者JohannaVal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 10:15:04