在mongo计数更多的值

特佐利尼奥

当时我该如何计算更多的值?我的收藏集看起来像这样,我想统计字段msgseverity

[
  {
    "msg": "A",
    "severity": "hello1",
    "group": "bb"
  },
  {
    "msg": "B",
    "severity": "hello1",
    "group": "bb"
  },
  {
    "msg": "A",
    "severity": "hello2",
    "group": "bb"
  },
  {
    "msg": "C",
    "severity": "hello2",
    "group": "bb"
  }
]

我想要得到的结果是这样的:

[
    [{
        "_id" : "A",
        "count" : 2.0
    },
    {
        "_id" : "B",
        "count" : 1.0
    },
    {
        "_id" : "C",
        "count" : 1.0
    }],
    [{
        "_id" : "hello1",
        "count" : 2.0
    },
    {
        "_id" : "hello2",
        "count" : 2.0
    }],
]

我尝试过的代码是:

db.getCollection('events').aggregate({
    $and: [
    {
        [
            { $unwind: "$msg" },
            {
                $group: {
                    _id: {$toLower: '$msg'},
                    count: { $sum: 1 }
                }
            }
        ]
    },
    {
        [
            { $unwind: "$severity" },
            {
                $group: {
                    _id: {$toLower: '$severity'},
                    count: { $sum: 1 },
                }
            }
        ]
    }]
}).toArray();

但是我得到这个错误:

错误:第5行:意外的令牌,

该代码从以下位置开始:

db.getCollection('events').aggregate([
    { $unwind: "$msg" },
    {
        $group: {
            _id: {$toLower: '$msg'},
            count: { $sum: 1 }
        }
    }
]).toArray();

并且它正常工作(仅适用于msg现场);我只是尝试结合2$unwind和2$group

这是一个片段

感谢您的时间!

Himanshu Sharma

我们可以使用$ facet在数据集上执行并行管道

以下查询可以为我们提供预期的输出:

db.collection.aggregate([
    {
        $facet:{
            "messagesInfo":[
                {
                    $group:{
                        "_id":{
                            $toLower:"$msg"
                        },
                        "msg":{
                            $first:"$msg"
                        },
                        "count":{
                            $sum:1
                        }
                    }
                },
                {
                    $project:{
                        "_id":0
                    }
                }
            ],
            "severityInfo":[
                {
                    $group:{
                        "_id":{
                            $toLower:"$severity"
                        },
                        "severity":{
                            $first:"$severity"
                        },
                        "count":{
                            $sum:1
                        }
                    }
                },
                {
                    $project:{
                        "_id":0
                    }
                }
            ]
        }
    }
]).pretty()

数据集:

{
    "_id" : ObjectId("5d8bb2751b53cab235fc20e0"),
    "msg" : "A",
    "severity" : "hello1",
    "group" : "bb"
}
{
    "_id" : ObjectId("5d8bb2751b53cab235fc20e1"),
    "msg" : "B",
    "severity" : "hello1",
    "group" : "bb"
}
{
    "_id" : ObjectId("5d8bb2751b53cab235fc20e2"),
    "msg" : "A",
    "severity" : "hello2",
    "group" : "bb"
}
{
    "_id" : ObjectId("5d8bb2751b53cab235fc20e3"),
    "msg" : "C",
    "severity" : "hello2",
    "group" : "bb"
}

输出:

{
    "messagesInfo" : [
        {
            "msg" : "C",
            "count" : 1
        },
        {
            "msg" : "B",
            "count" : 1
        },
        {
            "msg" : "A",
            "count" : 2
        }
    ],
    "severityInfo" : [
        {
            "severity" : "hello2",
            "count" : 2
        },
        {
            "severity" : "hello1",
            "count" : 2
        }
    ]
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章