我想在Mongo中使用mapreduce計數一個給定值顯示的次數。MongoDB Map Reduce顯示「[對象對象]」作爲鍵儘管類型檢查對他們
這裏是我的地圖功能,我測試,以確保每一個值的字符串:
function mapFunction() {
function normalizeDate(date) {
var day = date.getDay(),
month = date.getMonth(),
year = date.getYear();
return new Date(year, month, day);
}
if (this.events.event.toString() === "[object Object]"
|| typeof(this.events.event) !== 'string') {
throw new Error("Not a string...");
}
emit(normalizeDate(this.date), this.events.event);
}
這裏是我的降低作用,爲完整起見:
function reduceFunction (date, event_arry) {
return event_arry.reduce(function (a, b) {
if (a[b]) {
a[b]++;
}
else {
a[b] = 1;
}
return a;
}, {});
}
然後,我在mongo repl中運行mapReduce:
mongos> db.events.mapReduce(mapFunction, reduceFunction, {out: 'mr_test'})
{
"result" : "mr_test",
"timeMillis" : 148,
"counts" : {
"input" : 3481,
"emit" : 3481,
"reduce" : 82,
"output" : 14
},
"ok" : 1,
}
並且沒有錯誤,表明所有的event
s是string
。
然而,當我看輸出mr_test
集合中,我得到幾個條目是這樣的:
mongos> db.mr_test.find()
{ "_id" : ISODate("0113-04-05T00:00:00Z"), "value" : { "[object Object]" : 4 } }
{ "_id" : ISODate("0113-04-06T00:00:00Z"), "value" : { "[object Object]" : 5 } }
{ "_id" : ISODate("0113-04-30T00:00:00Z"), "value" : { "[object Object]" : 1, "eventTypeA" : 9, "eventTypeB" : 14, "eventTypeC" : 19 } }
是否有一個很好的解釋這個?如果是這樣,那是什麼?