我有一组这样的数据:
> db.esbtrans.find().limit(2).pretty()
{
"_id" : ObjectId("51fa56a509d013ddbd06f513"),
"messageflow" : "TEST",
"correlid" : "2b2bdc4f-24bc-412a-8438-9a7e0c256b38",
"start" : ISODate("2013-08-01T12:37:57.452Z"),
"finish" : ISODate("2013-08-01T12:38:17.452Z"),
"response" : NumberLong(20000),
"status" : "OK"
}
{
"_id" : ObjectId("51fa56a509d013ddbd06f514"),
"messageflow" : "TEST",
"correlid" : "0565d123-3570-4ce9-83d7-86e50aad48c5",
"start" : ISODate("2013-08-01T12:37:57.452Z"),
"finish" : ISODate("2013-08-01T12:38:44.452Z"),
"response" : NumberLong(47000),
"status" : "ERR"
}
{
"_id" : ObjectId("51fa56a509d013ddbd06f515"),
"messageflow" : "TEST2",
"correlid" : "d14c447a-eb4c-4a00-85fd-009955798386",
"start" : ISODate("2013-08-01T12:37:57.452Z"),
"finish" : ISODate("2013-08-01T12:38:57.452Z"),
"response" : NumberLong(60000),
"status" : "OK"
}
{
"_id" : ObjectId("51fa56a509d013ddbd06f516"),
"messageflow" : "TEST2",
"correlid" : "3b7902ce-a8bb-496a-a67f-23b562554c16",
"start" : ISODate("2013-08-01T12:37:57.452Z"),
"finish" : ISODate("2013-08-01T12:38:50.452Z"),
"response" : NumberLong(53000),
"status" : "ERR"
}
以下是成千上万条类似记录的两个元素,关键属性是“消息流”、“状态”和组合计数。我想得到一个看起来像这样的结果:
[{
"messageflow: "TEST",
"errors": 1,
"successes": 1
},{
"messageflow: "TEST2",
"errors": 1,
"successes": 1
}]
我已经达到了这样的聚合:
> db.esbtrans.aggregate(
{"$group":
{_id: {messageflow: "$messageflow", status: "$status"},
resptot: {$sum: "$response"},
count: {$sum: 1}}},
{"$project":
{flow: "$_id.messageflow",
status: "$_id.status",
count: "$count",
_id: 0}})
这会产生如下结果:
{
"result" : [
{
"count" : 240,
"flow" : "TEST2",
"status" : "ERR"
},
{
"count" : 267,
"flow" : "TEST",
"status" : "ERR"
},
{
"count" : 244,
"flow" : "TEST",
"status" : "OK"
},
{
"count" : 249,
"flow" : "TEST2",
"status" : "OK"
}
],
"ok" : 1
}
但是,我看不到如何将每个状态(“OK”或“ERR”)$project 到正确的输出上,以便它们是由“messageflow”标识的记录上的字段。有任何想法吗?