我有一组这样的数据:
> 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"," status"和组合的计数。我想得到一个看起来像这样的结果:
[{
"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"或#34; ERR")投射到正确的输出上,以便它们是已识别记录上的字段通过" messageflow"。有任何想法吗?
答案 0 :(得分:3)
Damo,你必须记住的一件事是,当你想按值分组时,你可能不得不使用$ cond运算符。
db.esbtrans.aggregate({
$group : {
_id : "$messageflow",
errors : { $sum : { $cond : [ { $eq : ["$status", "ERR"] } ,1,0] } },
successes : { $sum : { $cond : [ { $eq : ["$status", "OK"] } ,1,0] } },
}
})
解释:
我按messageflow
分组,因为此字段是您的基本轴。然后,为了计算错误和成功的数量,我将$sum
运算符与$cond
和$eq
结合使用。它只是比较status
是ERR还是OK并正确求和。