【问题标题】:Multiple accumulators in MongoDB Group Aggregation query for day,week,month and yearMongoDB Group Aggregation 中的多个累加器查询日、周、月和年
【发布时间】:2023-03-08 22:05:01
【问题描述】:

我正在尝试将我的数据按日、周、月和年分组在单个桩线中。我在不同的字段中有日期、周数、月份和年份。但我无法弄清楚如何在单个流中进行组聚合。 下面是 json 示例:

{ 
    "Ctrans" : {
    "status" : "active"
    }, 
    "Day-month" : "25-9", 
    "Month" : 9, 
    "Year" : 2019, 
    "Week" : 38
} 

我对此管道的查询:

db.getCollection("transaction").aggregate(
    [
        { 
            "$lookup" : {
                "from" : "customer", 
                "localField" : "pB.phone", 
                "foreignField" : "phone", 
                "as" : "Ctrans"
            }
        }, 
        { 
            "$unwind" : {
                "path" : "$Ctrans", 
                "includeArrayIndex" : "arrayIndex", 
                "preserveNullAndEmptyArrays" : false
            }
        }, 
        { 
            "$project" : {
                "date" : {
                    "$dateFromString" : {
                        "dateString" : "$createdAt", 
                        "onError" : "$date"
                    }
                }, 
                "Weekdate" : {
                    "$dateFromString" : {
                        "dateString" : "$createdAt", 
                        "onError" : "$date"
                    }
                }, 
                "Ctrans.status" : 1.0, 
                "createdAt" : 1.0
            }
        }, 
        { 
            "$project" : {
                "date" : {
                    "$dateToParts" : {
                        "date" : "$date"
                    }
                }, 
                "week" : {
                    "$week" : "$Weekdate"
                }, 
                "Ctrans.status" : 1.0
            }
        }, 
        { 
            "$project" : {
                "Day-month" : {
                    "$concat" : [
                        {
                            "$toString" : "$date.day"
                        }, 
                        "-", 
                        {
                            "$toString" : "$date.month"
                        }
                    ]
                }, 
                "Month" : "$date.month", 
                "Year" : "$date.year", 
                "Week" : "$week", 
                "Ctrans.status" : 1.0
            }
        }, 
        { 
            "$group" : {
                "_id" : {
                    "Today" : "$Day-month", 
                    "Status" : "$status"
                }, 
                "Total" : {
                    "$sum" : 1.0
                }
            }
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);

我的目标是对 :(Day-month, status),(month,status),(year,status),(week,status) 进行分组,并分别获取每个配对组的 count

【问题讨论】:

    标签: mongodb group-by mongodb-query aggregation-framework aggregation


    【解决方案1】:

    您可以使用$facet 在您的数据集上应用单独的$group 阶段,尝试:

    db.collection.aggregate([
        // current aggregation stages,
        {
            $facet: {
                "dayMonthStatus": [
                    { $group: { _id: { status: "$Ctrans.status", "dayMonth": "$Day-month" }, count: { $sum: 1 } } }
                ],
                "monthStatus": [
                    { $group: { _id: { status: "$Ctrans.status", "month": "$Month" }, count: { $sum: 1 } } }
                ],
                "yearStatus": [
                    { $group: { _id: { status: "$Ctrans.status", "year": "$Year" }, count: { $sum: 1 } } }
                ],
                "weekStatus": [
                    { $group: { _id: { status: "$Ctrans.status", "week": "$Week" }, count: { $sum: 1 } } }
                ]
            }
        }
    ])
    

    【讨论】:

    • 感谢您的解决方案。正是我正在尝试的
    • 我可以在每个组中添加不同的日期范围过滤器吗?我不知道怎么做?
    • @RahulAnand 是的,它就像一个单独的管道,因此您可以将 $match 放在每个组的前面
    猜你喜欢
    • 2010-10-05
    • 1970-01-01
    • 2011-12-29
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    相关资源
    最近更新 更多