我正在尝试测试mongoDb来管理我们的性能日志。
对于这个测试,我使用以下格式在集合中插入10M行
{ "_id" : ObjectId("4e9d3cc4621dc1dc11000000"), "date" : "Thu Oct 13 2011 15:37:21 GMT+0200 (CEST)", "loadtime" : 0.07, "msg" : "Lorem ipsum message" }
现在我希望能够得到每天的平均负载时间。
因此,根据我的理解,我需要对MapReduce进行两次传递。
第一个是创建一个天数的集合。
所以我试了
map = function() {
day = Date.UTC(this.date.getFullYear(), this.date.getMonth(), this.date.getDate());
emit({day: day}, {count: 1});
}
reduce = function(key, values) {
var count = 0;
values.forEach(function(v) {
count += v['count'];
});
return {count: count};
}
参考http://cookbook.mongodb.org/patterns/unique_items_map_reduce/
但是这会在几秒钟后杀死我的mongodb服务器。
db.loadTime.mapReduce(map, reduce, {out: 'days'});
Tue Oct 18 11:57:28 query failed : test.$cmd { mapreduce: "loadTime", map: function () {
day = Date.UTC(this.date.getFullYear(), this.date.ge..., reduce: function (key, values) {
var count = 0;
values.forEach(functio..., out: "days" } to: 127.0.0.1
Tue Oct 18 11:57:28 Error: error doing query: failed (anon):1509
这是我的错误日志
Tue Oct 18 11:56:08 [conn1] CMD: drop test.tmp.mr.mapreduce_1318931768_1_inc
55800/10000000 0%
112800/10000000 1%
171400/10000000 1%
229600/10000000 2%
288600/10000000 2%
345600/10000000 3%
404100/10000000 4%
462900/10000000 4%
522000/10000000 5%
579100/10000000 5%
629200/10000000 6%
677000/10000000 6%
724200/10000000 7%
767500/10000000 7%
818600/10000000 8%
864300/10000000 8%
921300/10000000 9%
972200/10000000 9%
1021600/10000000 10%
1070700/10000000 10%
1115600/10000000 11%
1163600/10000000 11%
1217400/10000000 12%
1269100/10000000 12%
1313300/10000000 13%
1366200/10000000 13%
Tue Oct 18 11:57:28 Got signal: 11 (Segmentation fault).
Tue Oct 18 11:57:28 Backtrace:
0x843a16d 0x842dbcd 0x741400 0x1eadcd
/usr/lib/mongodb/mongod(_ZN5mongo15printStackTraceERSo+0x2d) [0x843a16d]
/usr/lib/mongodb/mongod(_ZN5mongo10abruptQuitEi+0x3ed) [0x842dbcd]
[0x741400]
/usr/lib/xulrunner-2.0/libmozjs.so(+0xdadcd) [0x1eadcd]
我走的方向对吗?
我也遇到了同样的问题,解决方法如下:
代替
values.forEach(function(v) {
count += v['count'];
});
使用for(v in values){
count += v['count'];
}