在MongoDb中创建嵌套的聚合集

时间:2019-07-01 23:06:05

标签: mongodb mongodb-query aggregation-framework

我正在尝试对某些性能数据运行数据汇总报告查询。

EDIT2

我将以下示例文档替换为敏感数据。

[
{
    "_id" : ObjectId("5c8bf0903b46d40fc8fe79fb"),
    "URL" : "/path1.ashx?c={CVal}",
    "cs_host" : "sample.host.com",
    "Swimlane" : 1,
    "StartTime" : ISODate("2019-01-01T00:00:00.000Z"),
    "Average" : 0.03,
    "P_90" : 0.03,
    "URLCount" : 42
},
{
    "_id" : ObjectId("5c8bf0903b46d40fc8fe79fc"),
    "URL" : "/path1.ashx?c={CVal}",
    "cs_host" : "sample2.host.com",
    "Swimlane" : 1,
    "StartTime" : ISODate("2019-01-01T01:00:00.000Z"),
    "Average" : 0.05,
    "P_90" : 0.08,
    "URLCount" : 21
},
{
    "_id" : ObjectId("5c8bf0903b46d40fc8fe79fd"),
    "URL" : "/path1.ashx?c={CVal}",
    "cs_host" : "sample3.host.com",
    "Swimlane" : 2,
    "StartTime" : ISODate("2019-01-01T01:00:00.000Z"),
    "Average" : 0.03,
    "P_90" : 0.03,
    "URLCount" : 36
},
{
    "_id" : ObjectId("5c8bf0903b46d40fc8fe79fe"),
    "URL" : "/path1.ashx?c={CVal}",
    "cs_host" : "sample4.host.com",
    "Swimlane" : 3,
    "StartTime" : ISODate("2019-01-01T02:00:00.000Z"),
    "Average" : 0.05,
    "P_90" : 0.08,
    "URLCount" : 853
},
{
    "_id" : ObjectId("5c8bf0903b46d40fc8fe79ff"),
    "URL" : "/path2.ashx?c={CVal}",
    "cs_host" : "sample.host.com",
    "Swimlane" : 1,
    "StartTime" : ISODate("2019-01-01T03:00:00.000Z"),
    "Average" : 0.03,
    "P_90" : 0.03,
    "URLCount" : 67
},
{
    "_id" : ObjectId("5c8bf0903b46d40fc8fe79ab"),
    "URL" : "/path2.ashx?c={CVal}",
    "cs_host" : "sample2.host.com",
    "Swimlane" : 1,
    "StartTime" : ISODate("2019-01-01T02:00:00.000Z"),
    "Average" : 0.05,
    "P_90" : 0.08,
    "URLCount" : 37
},
{
    "_id" : ObjectId("5c8bf0903b46d40fc8fe79bb"),
    "URL" : "/path2.ashx?c={CVal}",
    "cs_host" : "sample3.host.com",
    "Swimlane" : 2,
    "StartTime" : ISODate("2019-01-01T01:00:00.000Z"),
    "Average" : 0.03,
    "P_90" : 0.03,
    "URLCount" : 17
},
{
    "_id" : ObjectId("5c8bf0903b46d40fc8fe79cb"),
    "URL" : "/path2.ashx?c={CVal}",
    "cs_host" : "sample4.host.com",
    "Swimlane" : 3,
    "StartTime" : ISODate("2019-01-01T00:00:00.000Z"),
    "Average" : 0.05,
    "P_90" : 0.08,
    "URLCount" : 96
}
]

我希望获得“每小时总计数的前5个URL的每个Swimlane”所描述的汇总。

生成的文档如下所示:

{
    "Swimlane": 1,
    "Hours": [
        "Hour": 0,
        "URLs": [
            "URL": "/path.ashx?c={CVal},
            "TotalCount": 63
        ]
    ]
}

编辑

产生查询结果的最接近查询是

[
    {
        $match: {
           StartTime: {
               $gt: ISODate("2019-06-27T07:00:00.000Z")
           }
        }
    },
    {
        $group: {
            _id: {swimlane: "$Swimlane", url: "$URL"},
            totalCount: {$sum: "$URLCount"}
        }
    },
    {
        $sort: { count: -1}
    },
    {
        $group: {
            _id: {swimlane: "$_id.swimlane"},
            urls: {
                $push : {
                    url: "$_id.url",
                    totalCount: "$totalCount"
                }
            }
        }
    },
    {
        $addFields:{
            urls:{
                $slice:["$urls",0,50]
            }
        }
    },
    {
        $sort: { "_id.swimlane" : 1}
    },
], { allowDiskUse: true}

为我生成以下报告

{
    "_id" : {
        "swimlane" : 0
    },
    "urls" : [ 
        {
            "url" : "/path.ashx?c={CVal}",
            "totalCount" : 56210
        }
    ]
}

0 个答案:

没有答案