I have following db values
{
"_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" : "[email protected]",
"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" : "[email protected]",
"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" : "[email protected]",
"clicked" : true
}
I need mongo aggregation query that get total deliver & not deliver result along with total email campaign count my mongo aggregate query is
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)
}
});
Got following result
{
"delivered": 2,
"not_delivered": 7,
"total_recipient": 9
}
How can i get "email_campaign_id" count without duplicate? by using above example its must be 2.
Expected output :
{ "delivered": 2, "not_delivered": 7, "total_recipient": 9, "total_campaign":2 }