I have a collection in MongoDB and their objects look like this:
Object 1
{
"_id" : ObjectId("5afde62a91952a2980a1b751"),
"notificationName" : "Teste Agendamento Pontual",
"messages" : [
{
"timestamp" : ISODate("2018-05-17T20:29:33.045Z"),
"message" : "Teste Agendamento Pontual"
},
{
"timestamp" : ISODate("2018-05-17T20:29:33.051Z"),
"message" : "Teste"
},
{
"timestamp" : ISODate("2018-05-17T20:29:44.680Z"),
"message" : "OK"
}
]
}
Object 2
{
"_id" : ObjectId("5afde62a9194322980a1b751"),
"notificationName" : "Teste Agendamento Pontual",
"messages" : [
{
"timestamp" : ISODate("2018-05-17T20:29:33.045Z"),
"message" : "Teste Agendamento Pontual"
},
{
"timestamp" : ISODate("2018-05-17T20:29:33.051Z"),
"message" : "NOT OK"
},
{
"timestamp" : ISODate("2018-05-17T20:29:44.680Z"),
"message" : "asdsadasd"
}
]
}
...
I'm trying to get a result grouped by notificationName with a count of objects with OK messages and other count with objects with NOT OK . I think I need to make a $cond in $group to check message property, but I'm not sure about that. And not sure if thats the best way to achieve this.