我一直在使用MongoDb一段时间,并认为它很棒。我有以下Activity对象。
[ Activity ]
"_id" : ObjectId("518a6c18e4b079d18de65b6e"),
"userUrl" : "http://news.bbc.co.uk",
"userAddress" : "1.2.3.4",
"requestTime" : ISODate("2013-05-08T15:15:35.780Z"),
"status" : "success"
注意,状态字段有大约5个不同的值,例如“错误”,“不可用”等
所以我现在处于负载高级别报告的阶段,但是在分组功能方面存在问题。例如,如果我插入以下测试数据: -
db.activity.insert({"userUrl": "http://news.bbc.co.uk", "userAddress" : "1.2.3.4", "requestTime" : new Date('Jan 01, 2013'), "status": "success"})
db.activity.insert({"userUrl": "http://news.bbc.co.uk", "userAddress" : "1.2.3.4", "requestTime" : new Date('Jan 01, 2013'), "status": "success"})
db.activity.insert({"userUrl": "http://news.bbc.co.uk", "userAddress" : "1.2.3.4", "requestTime" : new Date('Jan 01, 2013'), "status": "error"})
db.activity.insert({"userUrl": "http://news.bbc.co.uk", "userAddress" : "1.2.3.4", "requestTime" : new Date('Jan 01, 2013'), "status": "unavailable"})
db.activity.insert({"userUrl": "http://news.bbc.co.uk", "userAddress" : "1.2.3.4", "requestTime" : new Date('Jan 02, 2013'), "status": "success"})
db.activity.insert({"userUrl": "http://news.bbc.co.uk", "userAddress" : "1.2.3.4", "requestTime" : new Date('Jan 02, 2013'), "status": "error"})
db.activity.insert({"userUrl": "http://news.bbc.co.uk", "userAddress" : "1.2.3.4", "requestTime" : new Date('Jan 02, 2013'), "status": "error"})
db.activity.insert({"userUrl": "http://news.bbc.co.uk", "userAddress" : "1.2.3.4", "requestTime" : new Date('Jan 02, 2013'), "status": "unavailable"})
db.activity.insert({"userUrl": "http://news.bbc.co.uk", "userAddress" : "1.2.3.4", "requestTime" : new Date('Jan 02, 2013'), "status": "unavailable"})
...我运行这个功能......
db.activity.group(
{
$keyf : function(doc) {
return { "date" : doc.requestTime.getDate() + "/" + doc.requestTime.getMonth() + "/" + doc.requestTime.getFullYear()};
},
initial: { count:0 },
reduce: function(obj, prev) { prev.count++; }
})
......我得到了这个结果......
[
{
"date" : "1/0/2013",
"count" : 4
},
{
"date" : "2/0/2013",
"count" : 5
}
]
对于仅仅在某个日期进行分组非常有效。 我的问题是,我如何对状态的日期(按日)进行分组?例如。得到这样的东西是理想的: -
[
{
"date" : "1/0/2013",
"success" : 2,
"error": 1,
"unavailable": 1
},
},
{
"date" : "2/0/2013",
"success" : 1,
"error": 2,
"unavailable": 2
}
]
......或......
[
{
"date" : "1/0/2013",
status: [
"success" : 2,
"error": 1,
"unavailable": 1
]
},
},
{
"date" : "2/0/2013",
status: [
"success" : 1,
"error": 2,
"unavailable": 2
]
}
]
答案 0 :(得分:0)
只要你有一组选择的值来对它们进行硬编码,这对于聚合框架来说是很有可能的。通常,无法向键提升值。
这是A / F调用:
db.activity.aggregate(
{ $group: {
_id: { date: '$requestTime' },
success : {
$sum: { $cond: [ { $eq: [ 'success', '$status' ] } , 1, 0 ] }
},
error : {
$sum: { $cond: [ { $eq: [ 'error', '$status' ] } , 1, 0 ] }
},
unavailable : {
$sum: { $cond: [ { $eq: [ 'unavailable', '$status' ] } , 1, 0 ] }
}
} }
);
哪个输出:
{
"result" : [
{
"_id" : {
"date" : ISODate("2013-01-02T00:00:00Z")
},
"success" : 1,
"error" : 2,
"unavailable" : 2
},
{
"_id" : {
"date" : ISODate("2013-01-01T00:00:00Z")
},
"success" : 2,
"error" : 1,
"unavailable" : 1
}
],
"ok" : 1
}