MongoDb聚合计数distict字段值

时间:2017-01-12 04:05:38

标签: mongodb mongoose mongodb-query aggregation-framework

我有以下数据库值

{ 
    "_id" : ObjectId("5875d82f8447f454b37a947b"), 
    "updatedAt" : ISODate("2017-01-11T07:01:03.570+0000"), 
    "createdAt" : ISODate("2017-01-11T07:01:03.570+0000"), 
    "__v" : NumberInt(0), 
    "activity_type" : "email_campaign_delivery", 
    "customer_id" : ObjectId("581ad6f4d1fb4a0e14af159c"), 
    "email_campaign_id" : ObjectId("5875d82f8447f454b37a9476"), 
    "email" : "jpnew@tblr.com", 
    "delivered" : true
}
{ 
    "_id" : ObjectId("5875d82f8447f454b37a947c"), 
    "updatedAt" : ISODate("2017-01-11T07:01:03.570+0000"), 
    "createdAt" : ISODate("2017-01-11T07:01:03.570+0000"), 
    "__v" : NumberInt(0), 
    "activity_type" : "email_campaign_delivery", 
    "customer_id" : ObjectId("581ad6f4d1fb4a0e14af159c"), 
    "email_campaign_id" : ObjectId("5875d82f8447f454b37a9476"), 
    "email" : "tyt@dffd.com", 
    "delivered" : false
}
{ 
    "_id" : ObjectId("5875d82f8447f454b37a947d"), 
    "updatedAt" : ISODate("2017-01-11T07:01:03.570+0000"), 
    "createdAt" : ISODate("2017-01-11T07:01:03.570+0000"), 
    "__v" : NumberInt(0), 
    "activity_type" : "email_campaign_click", 
    "customer_id" : ObjectId("581ad6f4d1fb4a0e14af159c"), 
    "email_campaign_id" : ObjectId("5875d82f8447f454b37a9475"), 
    "email" : "tyt@dffd.com", 
    "clicked" : true
}

我需要mongo聚合查询才能获得总交付量&不提供结果以及总电子邮件广告系列计数 我的mongo聚合查询是

CampaignActivity.aggregate([{
                        "$group": {
                                "_id": null,
                                "delivered": {
                                    $sum: {
                                        "$cond": {
                                            if: {
                                                $eq: ["$delivered", true]
                                            },
                                            then: 1,
                                            else: 0
                                        }
                                    }
                                },
                                "not_delivered": {
                                    $sum: {
                                        "$cond": {
                                            if: {
                                                $eq: ["$delivered", false]
                                            },
                                            then: 1,
                                            else: 0
                                        }
                                    }
                                },
                                "total_recipient": {
                                    $sum: {
                                        "$cond": {
                                            if: {
                                                $eq: ["$activity_type", "email_campaign_delivery"]
                                            },
                                            then: 1,
                                            else: 0
                                        }
                                    }
                                }


                            }

                    }, {
                        "$project": {
                            "_id": 0,
                            "not_delivered":1,
                            "total_recipient":1,
                            "delivered": 1
                        }
                    }],function (err, result) {
                                if (err) {
                                    console.log(err)
                                } else {
                                    console.log(result)
                                }
                    });

得到以下结果

{
  "delivered": 2,
  "not_delivered": 7,
  "total_recipient": 9
}

如何在没有重复的情况下获得“email_campaign_id”计数?通过使用上面的例子,它必须是2。

预期产出:

{ "delivered": 2, "not_delivered": 7, "total_recipient": 9, "total_campaign":2 }

2 个答案:

答案 0 :(得分:2)

您可以做的是在群组阶段将电子邮件地址添加到数组中,我使用$addToSet因此不会添加重复项。在下一阶段计划值并使用$size显示唯一电子邮件地址的数量。在我的例子下面。

db.collection.aggregate(
    [
        {
            $group: {
                                            "_id": null,
                                            "delivered": {
                                                $sum: {
                                                    "$cond": {
                                                        if: {
                                                            $eq: ["$delivered", true]
                                                        },
                                                        then: 1,
                                                        else: 0
                                                    }
                                                }
                                            },
                                            "not_delivered": {
                                                $sum: {
                                                    "$cond": {
                                                        if: {
                                                            $eq: ["$delivered", false]
                                                        },
                                                        then: 1,
                                                        else: 0
                                                    }
                                                }
                                            },
                                            emails : { $addToSet: "$email"}
                                        }
        },
        {
            $project: {
            delivered :1,
            not_delivered : 1,
            total_reciepient : { $size: "$emails" }
            }
        },

    ]    
);

答案 1 :(得分:0)

将您需要的不同项目分组到一个数组中,然后使用$ size来获取该数组。 $ size可与$ aggregate一起使用

https://docs.mongodb.com/manual/reference/operator/aggregation/size/