次のような一連のデータがあります。
> 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"
}
これは、何万もの同様のレコードの 2 つの要素です。主要なプロパティは、「メッセージフロー」、「ステータス」、および組み合わせの数です。次のような結果を得たいです。
[{
"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」で識別されるレコードのフィールドにする方法がわかりません。何か案は?