Mongodb聚合-当月匹配

时间:2019-06-12 18:53:15

标签: mongodb aggregation-framework

我正在定义一个mongodb聚合表达式,该表达式将存储在JSON中并执行。 当前的聚合表达式主要是简单的分组和投影。

例如下面是跟进日期分组的汇总查询

[{
            "$group": {
                "_id": {
                    "day_of_month": {
                        "$dayOfMonth": "$data.follow_up_date"
                    },
                    "month": {
                        "$month": "$data.follow_up_date"
                    },
                    "year": {
                        "$year": "$data.follow_up_date"
                    }
                },
                "total_leads": {
                    "$sum": 1
                },
                "total_loan_amount": {
                    "$sum": "$data.loan_amount"
                },
                "follow_up_date": {
                    "$push": "$data.follow_up_date"
                },
                "data": {
                    "$push": "$data"
                }
            }
        },
        {
            "$project": {
                "_id": 1,
                "day_of_month": "$_id.day_of_month",
                "month": "$_id.month",
                "year": "$_id.year",
                "total_leads": "$total_leads",
                "total_loan_amount": "$total_loan_amount",
                "follow_up_date": {
                    "$arrayElemAt": ["$follow_up_date", 0]
                },
                "data": "$data"
            }
        }]


在此聚合表达式中,我想添加基于动态日期的过滤器,该过滤器将允许我仅获取当前月份或当前月份的日期。

例如

[{
            "$group": {
                "_id": {
                    "day_of_month": {
                        "$dayOfMonth": "$data.follow_up_date"
                    },
                    "month": {
                        "$month": "$data.follow_up_date"
                    },
                    "year": {
                        "$year": "$data.follow_up_date"
                    }
                },
                "total_leads": {
                    "$sum": 1
                },
                "total_loan_amount": {
                    "$sum": "$data.loan_amount"
                },
                "follow_up_date": {
                    "$push": "$data.follow_up_date"
                },
                "data": {
                    "$push": "$data"
                }
            }
        },
    {
      //something like this 
      "_id.day_of_month" : {$month : new Date()}
    },
        {
            "$project": {
                "_id": 1,
                "day_of_month": "$_id.day_of_month",
                "month": "$_id.month",
                "year": "$_id.year",
                "total_leads": "$total_leads",
                "total_loan_amount": "$total_loan_amount",
                "follow_up_date": {
                    "$arrayElemAt": ["$follow_up_date", 0]
                },
                "data": "$data"
            }
        }]

是否可以添加动态日期过滤器?

1 个答案:

答案 0 :(得分:1)

有2种方法,尽管我建议两种方法都建议在$match阶段之前进行初始$group,以限制通过其余管道传递的数据量只需一个子集。

最简单的方法可能是先算出您要查询的日期,然后开始/结束您感兴趣的特定日期或月份的日期,并对此进行比较检查:

{
    '$match': {
        '$data.follow_up_date': {
            '$lte': ...
            '$gte': ...
        }
    }
}

否则,您可以使用$expr来获取跟踪日期和当前日期并比较一些格式化的值,所以:

{
    '$match': {
        '$expr': {
            '$eq': [
                { "$dateToString": { format: "%Y-%m-%d", date: "$data.follow_up_date" }},
                { "$dateToString": { format: "%Y-%m-%d", date: new Date() }},
             ]
        } 
    }
}

然后,您只需要更改格式即可更改比较的特异性级别。

我个人建议第一个,因为它处理较少,并且是实际的日期比较,而不是字符串比较,但是两者都可以工作