var mapFunction = function() {
emit(this.waybillId, this.createTime)
};
var reduceFunction = function(key, values) {
//values有多个,才会进入这个方法
var time = "";
for(var i = 0; i < values.length; i++){
if(time == "" || time < values[i]){
time = values[i];
}
}
return {createTime:time};
};
var finalizeFunction = function(key, reducedValue) {
//如果有createTime证明有进入reduce处理过,直接返回最终值
if(reducedValue.createTime){
return reducedValue
} else{
//没有createTime证明没有进入reduce方法处理,构造一下返回的格式
return {createTime:reducedValue};
}
};
db.runCommand(
{
mapReduce: "waybillLogisticsLog",
map: mapFunction,
reduce: reduceFunction,
finalize: finalizeFunction,
out: "emp_res",
query: {"nodeType": {$in: ["abnormal", "normal", "TMS_SIGN"]}}
}
)
如果不指定finalize,默认返回的数据是以key的值为_id,reduceFunction 返回的值作为value,效果如下
{
"_id" : " 1zts19052102164-ffh3",
"value" : {
"createTime" : Timestamp(1560337402, 117)
}
}