Mongo で mapReduce を使用して、特定の値が表示される回数を数えようとしています。
すべての値が文字列であることを確認するためにテストするマップ関数を次に示します。
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);
}
完全を期すために、これが私のreduce関数です。
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
のタイプが であったことが示唆されます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 } }
これについて良い説明はありますか?もしそうなら、それは何ですか?