获取“loglevel"的计数对于每个“名称" [英] Get count of "loglevel" for each "name"

查看:10
本文介绍了获取“loglevel"的计数对于每个“名称"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我有以下收藏:

db.names.find({})

db.names.find({})

{ "_id" : ObjectId("5768d9b4bc6f464899594570"), "name" : "t1", "loglevel" : "ERROR" }

{ "_id" : ObjectId("5768d9d5bc6f464899594571"), "name" : "t1", "loglevel" : "ERROR" }

{ "_id" : ObjectId("5768d9dcbc6f464899594572"), "name" : "t1", "loglevel" : "WARNING" }

{ "_id" : ObjectId("5768d9dfbc6f464899594573"), "name" : "t1", "loglevel" : "WARNING" }

{ "_id" : ObjectId("5768d9e0bc6f464899594574"), "name" : "t1", "loglevel" : "WARNING" }

{ "_id" : ObjectId("5768d9e7bc6f464899594575"), "name" : "t1", "loglevel" : "INFO" }

{ "_id" : ObjectId("5768d9f3bc6f464899594576"), "name" : "t2", "loglevel" : "INFO" }

{ "_id" : ObjectId("5768d9f9bc6f464899594577"), "name" : "t2", "loglevel" : "ERROR" }

{ "_id" : ObjectId("5768da19bc6f464899594578"), "name" : "t2", "loglevel" : "ERROR" }

{ "_id" : ObjectId("5768da1abc6f464899594579"), "name" : "t2", "loglevel" : "ERROR" }

{ "_id" : ObjectId("5768da1bbc6f46489959457a"), "name" : "t2", "loglevel" : "ERROR" }

{ "_id" : ObjectId("5768e247bc6f46489959457b"), "name" : "t3", "loglevel" : "INFO" }

我试过了,但我认为应该是更好的解决方案(else也可以省略)

I have tried but I think it should be better solution (also else can be omitted)

db.names.aggregate(
    {$group: {
        _id: "$name",
        error: {$sum: {$cond: {if: {$eq: ['$loglevel', 'ERROR']}, then: {$sum: 1}, else: {$sum: 0}}}},
        warning: {$sum: {$cond: {if: {$eq: ['$loglevel', 'WARNING']}, then: {$sum: 1}, else: {$sum: 0}}}},
        info: {$sum: {$cond: {if: {$eq: ['$loglevel', 'INFO']}, then: {$sum: 1}, else: {$sum: 0}}}}
    }}
)

推荐答案

去掉 { "$sum": 1 }{ "$sum": 0 }if/else 条件块中的表达式,将它们替换为值 1 和 0(分别用于每个条件块).

Remove the { "$sum": 1 } and { "$sum": 0 } expressions in your if/else conditional blocks, substitute them with the values 1 and 0 (respectively for each conditional block).

最终的管道应该是这样的,使用另一个 $cond 语法省略了 if/else 块:

The final pipeline should look like this, using the other $cond syntax which omits the if/else blocks:

db.names.aggregate([
    {
        "$group": {
            "_id": "$name",
            "error": { 
               "$sum": { 
                   "$cond": [ { "$eq": [ "$loglevel",  "ERROR" ] }, 1, 0] 
               }
           },
           "warning":{
               "$sum": { 
                   "$cond": [ { "$eq": [ "$loglevel", "WARNING" ] }, 1, 0 ]
                }
           },
           "info": { 
               "$sum": { 
                   "$cond": [ { "$eq": [ "$loglevel",  "INFO" ] }, 1, 0 ]
               }
           }
        }
    }
])

<小时>

或者动态创建管道,给定一组可能的状态:


Or dynamically create the pipeline, given an array of possible statuses:

var statuses = ["ERROR", "WARNING", "INFO"],
    groupOperator = { "$group": { "_id": "$name" } };

statuses.forEach(function (status){ 
    groupOperator["$group"][status.toLowerCase()] = { 
       "$sum": { 
           "$cond": [ { "$eq": [ "$loglevel",  status ] }, 1, 0] 
       }
   }
});

db.names.aggregate([groupOperator]);

输出

/* 1 */
{
    "_id" : "t1",
    "error" : 2,
    "warning" : 3,
    "info" : 1
}

/* 2 */
{
    "_id" : "t2",
    "error" : 4,
    "warning" : 0,
    "info" : 1
}

/* 3 */
{
    "_id" : "t3",
    "error" : 0,
    "warning" : 0,
    "info" : 1
}

这篇关于获取“loglevel"的计数对于每个“名称"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆