Prova il seguente codeRefer - https://docs.mongodb.com/manual /riferimento/operatore/aggregazione/eq/
https://docs.mongodb.com/manual/reference/operator/ aggregazione/cond/
https://docs.mongodb.com/manual/reference/operator/ aggregazione/gruppo/
DB.aggregate([
{$project:
{
tag_id: '$tag_id',
unreadcount: { $cond: [ { $eq: [ '$unread', 'true' ] }, 1, 0 ] }
}},
{ $group: {
_id: '$tag_id',
unread: { $sum: '$unreadcount'},
}}
], function (err, results) {
console.log(results);
})