I have a collection with documents in the following format:
{
"_id" : ObjectId("abc"),
"mobile" : "123456789",
"channels" : [
{
"name" : "email.xz",
"type" : "a",
"properties" : [
{
"provider" : "outlook"
}
]
},
{
"name" : "sms",
"type" : "sms"
}
],
}
I need to find customers who have at least one channel which the name ends with ".xz", but only if provider exists and has a value, and return the count of customers per channel. For instance:
email.xz = 10
sms.xz = 5
push.xz = 7
I tried to use the aggregate function below, but it resulted in a count of all the customers that match the criteria.
db.consumers.aggregate(
{$match: {"channels.name": {$regex: ".xz$"},
"notificationChannels.properties.provider": { $exists: true }}},
{$group: {_id: "channels.name"},
count: {$sum: 1}})