MongoDB:使用组对数组进行计数

MongoDB:使用组对数组进行计数,mongodb,Mongodb,我需要一种对数组进行分组和计数的方法。 我想要实现的是按“类型”返回计数 下面是我的部门文档的外观 { "_id" : ObjectId("1234566789"), "jobStatus" : "OK", "request" : { "header" : { "timestamp&q

我需要一种对数组进行分组和计数的方法。 我想要实现的是按“类型”返回计数

下面是我的部门文档的外观

{
    "_id" : ObjectId("1234566789"),
    "jobStatus" : "OK",
    "request" : 
     {
        "header" : {
                "timestamp" : "2020-10-21 00:00:00.000Z",
                "action" : "importAction",
                "type" : "registration"
            },
        "body" : [{
                "system_header" : {
                    "student" : "2019051",
                    "course" : "Business"
                }
              }]
     }
}
当指望 我用下面的代码做了第一次搜索

db.department.aggregate(
    [{
        $match: {
            sysDateI: {
                $gt: ISODate("2020-10-18 22:00:00.000Z"),
                $lt: ISODate("2020-10-19 22:00:00.000Z")
            }
        }
    }, {
    $group: {
        _id: '$request.header.type',
        COUNT: {
            $sum: 1
                    }
        }
    }]
)
并产生了以下令人满意的结果

/* 1 */
{
    "_id" : "alumni",
    "COUNT" : 3
}

/* 2 */
{
    "_id" : "graduation",
    "COUNT" : 5
}

/* 3 */
{
    "_id" : "registration",
    "COUNT" : 43
}
但是,当我尝试使用下面的函数计算“body”部分的内容时,它返回数组的内容,而不是求和

代码

db.department.aggregate(
    [{
        $match: {
            sysDateI: {
                $gt: ISODate("2020-10-18 22:00:00.000Z"),
                $lt: ISODate("2020-10-19 22:00:00.000Z")
            }
        }
    }, {
    $group: {
        _id: '$request.body.system_header.course',
        COUNT: {
            $sum: 1
                    }
        }
    }]
)
结果如下:

{
    "_id":[ 
    "Education", 
    "Education", 
    "Education", 
    "Business", 
    "Science"
    ],
    "COUNT" : 1.0
}
我需要归还的是这样的东西。我知道system_头是一个数组

{
    "_id":"Business",
    "COUNT":45
}

{
    "_id":"Education",
    "COUNT":3
}

您的指导将不胜感激

Hi add$unwind before group,如下所示:

db.department.aggregate(
    [{
        $match: {
            sysDateI: {
                $gt: ISODate("2020-10-18 22:00:00.000Z"),
                $lt: ISODate("2020-10-19 22:00:00.000Z")
            }
        }
    }, 
    {
             $unwind:  "$request.body"
     },
    {
    $group: {
        _id: '$request.body.system_header.course',
        COUNT: {
            $sum: 1
                    }
        }
    }]
)

Hi在组之前添加$REWIND,如下所示:

db.department.aggregate(
    [{
        $match: {
            sysDateI: {
                $gt: ISODate("2020-10-18 22:00:00.000Z"),
                $lt: ISODate("2020-10-19 22:00:00.000Z")
            }
        }
    }, 
    {
             $unwind:  "$request.body"
     },
    {
    $group: {
        _id: '$request.body.system_header.course',
        COUNT: {
            $sum: 1
                    }
        }
    }]
)

只需在$group阶段之前使用
{$unwind:$request.body}
只需在$group阶段之前使用
{$unwind:$request.body}
只需在$group阶段之前使用
{$unwind:$request.body}
只需在$group阶段之前使用
解构身体。您提供的解决方案提供了所需的输出。非常感谢您,先生。您提供的解决方案提供了所需的输出。