0

我有一组这样的数据:

> 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”标识的记录上的字段。有任何想法吗?

4

1 回答 1

3

达摩,您必须记住的一件事是,当您想按值分组时,您可能必须使用 $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运算符与$condand结合使用$eq。它只是比较status是 ERR 还是 OK 并正确求和。

于 2013-08-01T13:16:59.287 回答