mongo group by次

时间:2019-04-05 11:47:33

标签: mongodb aggregation-framework

假设我有这组数据:

{ ValidFrom: "2019-03-25T16:01:55.714+0000", ValidTo: "2019-03-25T16:01:55.714+0000" },    
{ ValidFrom: "2019-03-26T16:01:55.714+0000", ValidTo: "2019-03-25T16:01:55.714+0000" },    
{ ValidFrom: "2019-03-25T16:01:55.714+0000", ValidTo: "2019-03-27T16:01:55.714+0000" }

我希望通过一个查询看到此结果:

{ "Day": "2019-03-25", ValidFromCount: 2, ValidToCount: 2 },    
{ "Day": "2019-03-26", ValidFromCount: 1, ValidToCount: 0 },    
{ "Day": "2019-03-27", ValidFromCount: 0, ValidToCount: 1 }

当前,我编写了此汇总,但现在被困住了:

 {
     $addFields: {
            ValidFromDay: { $dateToString: { format: "%Y-%m-%d", date: "$ValidFrom" } },
            ValidUntilDay: { $dateToString: { format: "%Y-%m-%d", date: "$ValidUntil" } }
     }
 },
 {
     $group : {
        _id: { FromDate: '$ValidFromDay', ToDate: '$ValidUntilDay' },
        Count: { "$sum": 1 },
     }
 }, 
 {
     $group : {
        _id: null,
        FromDates: { "$addToSet": { "Date": "$_id.FromDate", "FromCount": { "$sum": "$Count" } } },
        ToDate: { "$addToSet": { "Date": "$_id.ToDate", "UntilCount": "$Count" } }
     }
 }

是否可以通过某种方式产生我想要的结果?

2 个答案:

答案 0 :(得分:2)

您需要添加2个字段的数组,而不仅仅是2个字段。它可以让您放松并按日期进行计数:

{
     $addFields: {
            boundary: [
                { day: {$dateToString: { format: "%Y-%m-%d", date: "$ValidFrom" } }, from: 1 },
                { day: { $dateToString: { format: "%Y-%m-%d", date: "$ValidTo" } } , to: 1 }
                ]
     }
},
{
    $unwind: "$boundary"
},
{
    $group: {
        _id: "$boundary.day",
        ValidFromCount: {$sum: "$boundary.from"},
        ValidToCount: {$sum: "$boundary.to"},
    }
}

答案 1 :(得分:0)

我认为这可以满足您的需求。流水线分为三个阶段。一个$project,用于构造单独的日,月和年字段。

> projector
{
    "$project" : {
        "day" : {
            "$dayOfMonth" : "$ValidFrom"
        },
        "month" : {
            "$month" : "$ValidFrom"
        },
        "year" : {
            "$year" : "$ValidFrom"
        },
        "ValidFrom" : 1
    }
}

然后创建一个$group,并使用_id中的{year, month, day}按天计算总数。

> grouper
{
    "$group" : {
        "_id" : {
            "year" : "$year",
            "month" : "$month",
            "day" : "$day"
        },
        "ValidFromCount" : {
            "$sum" : 1
        },
        "ValidToCount" : {
            "$sum" : 1
        }
    }
}

最后,进行投影以消除虚假字段,并使Day字段变为所需的格式。

> converter
{
    "$project" : {
        "_id" : 0,
        "Day" : {
            "$concat" : [
                {
                    "$toString" : "$_id.year"
                },
                "-",
                {
                    "$toString" : "$_id.month"
                },
                "-",
                {
                    "$toString" : "$_id.day"
                }
            ]
        },
        "ValidFromCount" : 1,
        "ValidToCount" : 1
    }
}

运行即可执行(我在集合so2中创建了您的数据):

> db.so2.find()
{ "_id" : ObjectId("5ca75adfd1a64a2919883a8d"), "ValidFrom" : "2019-03-25T16:01:55.714+0000", "ValidTo" : "2019-03-25T16:01:55.714+0000" }
{ "_id" : ObjectId("5ca75adfd1a64a2919883a8e"), "ValidFrom" : "2019-03-26T16:01:55.714+0000", "ValidTo" : "2019-03-25T16:01:55.714+0000" }
{ "_id" : ObjectId("5ca75adfd1a64a2919883a8f"), "ValidFrom" : "2019-03-25T16:01:55.714+0000", "ValidTo" : "2019-03-27T16:01:55.714+0000" }
>
> db.so3.aggregate([projector,grouper,converter])
{ "ValidFromCount" : 1, "ValidToCount" : 1, "Day" : "2019-3-26" }
{ "ValidFromCount" : 2, "ValidToCount" : 2, "Day" : "2019-3-25" }
>

我不确定您提供的测试数据是否正确,因为第二个文档似乎已及时返回,因此ValidTo位于ValidFrom之前。