在mongodb中聚合,匹配中有多个条件

时间:2018-01-26 21:13:27

标签: mongodb mongodb-query aggregation-framework

我有以下文件

{
        "_id" : "server1-1509181203-1",
        "V" : 0,
        "mp" : [
                "server1-1509181203-1"
        ],
        "cD" : ISODate("2017-10-28T09:00:03Z"),
        "wD" : ISODate("2017-10-28T09:00:03.838Z"),
        "asv" : 1,        
        "fromAddress" : "noreply@example.com",
        "fromUserId" : "noreply",
        "mailKind" : "custom",        
},
{
        "_id" : "server1-1509181203-1",
        "V" : 0,
        "mp" : [
                "server1-1509181203-1"
        ],
        "cD" : ISODate("2017-10-28T09:00:03Z"),
        "wD" : ISODate("2017-10-28T09:00:03.838Z"),
        "asv" : 1,        
        "fromAddress" : "noreply@example.com",
        "fromUserId" : "noreply",
        "mailKind" : "custom",        
},
{
        "_id" : "server2-1529181203-1",
        "V" : 0,
        "mp" : [
                "server2-1529181203-1"
        ],
        "cD" : ISODate("2017-10-27T09:00:03Z"),
        "wD" : ISODate("2017-10-27T09:00:03.838Z"),
        "asv" : 1,        
        "fromAddress" : "noreply@example.com",
        "fromUserId" : "noreply",
        "mailKind" : "standard",        
},

目标是按邮件分组计算每天发送的电子邮件数

预期结果: -

{ "_id" : { "mailKind" : "custom", "DayOftheMonth" : 26 }, "cnt" : 2 }
{ "_id" : { "mailKind" : "standard", "DayOftheMonth" : 27 }, "cnt" : 1 }

我的查询是: -

db.Collection.aggregate({ $match : { "cD" : { $gte: ISODate("2018-01-26T00:00:00.000Z")} } },{ $group : { _id : { mailKind : "$mailKind" , DayOftheMonth : { $dayOfMonth : "$cD"}}, cnt : {$sum : 1 } } })

问题: -

以上查询适用于所有mailKind,但我只想申请某种类型。 当我尝试以下查询时出现错误

db.Example.aggregate({ $match : { "cD" : { $gte: ISODate("2018-01-25T00:00:00.000Z")} }, "mailKind" : [ "custom", "standard"] },{ $group : { _id : { mailKind : "$mailKind" , DayOftheMonth : { $dayOfMonth : "$cD"}}, cnt : {$sum : 1 } } })

我们不能在匹配表达式中使用两个或更多条件吗?有人可以帮忙。

0 个答案:

没有答案