mongodb Groupby聚合查询中的日期范围过滤器

时间:2019-11-18 10:01:51

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

我想按不同的日期范围过滤每个组聚集。对于 dayMonthStatus ,我想对 monthStatus 过滤 $ currentDate-1 。 >-当前月份数,持续周状态  -当前星期数

示例json:

{ 
    "createdAt" : "2019-10-02T04:55:13.472Z", 
    "Day-month" : "2-10", 
    "Month" : NumberInt(10), 
    "Year" : NumberInt(2019), 
    "Week" : NumberInt(39)
}

我尝试过$ cond运算符,但只得到空值或错误“ errmsg”:“表示表达式的对象必须恰好具有一个字段” ,下面是我想要的groupby聚合代码应用过滤器。

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 } } }
            ]
        }
    }
])

我已尝试以以下格式在groupby中使用$ match:

"dayMonthStatus": [
                { $group: { _id: { status: "$Customer-transaction.status", "dayMonth": "$Day-month" }, count: { $sum: 1 },
                     } },{ $match: {"dayMonth": { '$gte': "1-10", '$lt': "3-10"}}}
            ]

0 个答案:

没有答案