字符串在mapReduce中

时间:2017-08-14 19:10:01

标签: javascript mongodb mapreduce

我正在尝试计算我的收藏中每天的文档数量。我使用mongoDB mapReduce函数与下面的map和reduce函数:

var tsMapper = function(){
    // Get the timestamp and convert it to Date
    var ts = new Date((this.ntimestamp_ms));
    var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
    var year = ts.getFullYear();
    var month = months[ts.getMonth()];
    var date = ts.getDate();
    var newTS = month + ' ' + date + ' ' + year;
    // emit the newTS and value as 1
    emit(String(newTS), 1);
   }

和Reducer功能:

var reducer = function(key,values){
    return Array.sum(values);
    }

然后当我在我的数据库上运行代码时:

db.test.mapReduce(tsMapper, reducer, {"out" : "ts_count"})

它只返回一个包含一个文档的集合,其中“_id”为“undefined NaN NaN”,其值等于文档总数:

{ "_id" : "undefined NaN NaN", "value" : 6 }

有人可以告诉我为什么newTS字符串变成“未定义的NaN NaN”吗?

0 个答案:

没有答案