Map Reduceを使用して、日付ごとのフィールド値の1つに従ってドキュメント数をカウントしようとしています。まず、いくつかの通常のfind()関数の結果を次に示します。
db.errors.find({ "cDate" : ISODate("2012-11-20T00:00:00Z") }).count();
579を返します(つまり、この日付には579のドキュメントがあります)
db.errors.find( { $and: [ { "cDate" : ISODate("2012-11-20T00:00:00Z") }, {"Type":"General"} ] } ).count()
443を返します(つまり、Type = "General"の場合、この日付には443のドキュメントがあります)
以下は私のMapReduceです:
db.runCommand({ mapreduce: "errors",
map : function Map() {
emit(
this.cDate,//Holds a date value
{
count: 1,
countGeneral: 1,
Type: this.Type
}
);
},
reduce : function Reduce(key, values) {
var reduced = {count:0,countGeneral:0,Type:''};
values.forEach(function(val) {
reduced.count += val.count;
if (val.Type === 'General')
reduced.countGeneral += val.countGeneral;
});
return reduced;
},
finalize : function Finalize(key, reduced) {
return reduced;
},
query : { "cDate" : { "$gte" : ISODate("2012-11-20T00:00:00Z") } },
out : { inline : 1 }
});
日付20-11-20の場合、マップはリターンを減らします。
count: 579
countGeneral: 60 (should be 443 according to the above find query)
今、私は、Reduceがループする方法で予測できないことを理解しているので、これをどのように行う必要がありますか?ありがとう