Database
[{comment:{text_sentiment:"positive",topic:"A"}},/** DOC-1*/{comment:{text_sentiment:"negative",topic:"A"}},/** DOC-2*/{comment:{text_sentiment:"positive",topic:"B"}}]
Query
db.collection.aggregate([{$match:{/**_id: "xyz",*/"comment.topic":{$exists:1},}},{$group:{_id:"$comment.topic",total:{$sum:1},text_sentiments:{$push:"$comment.text_sentiment"}}},{$project:{topic:"$_id",topicOccurance:"$total",sentiment:{"positive":{$reduce:{input:"$text_sentiments",initialValue:0,in:{$sum:["$$value",{"$cond":{"if":{$eq:["$$this","positive"]},"then":1,"else":0}}]}}},"negative":{$reduce:{input:"$text_sentiments",initialValue:0,in:{$sum:["$$value",{"$cond":{"if":{$eq:["$$this","negative"]},"then":1,"else":0}}]}}},"neutral":{$reduce:{input:"$text_sentiments",initialValue:0,in:{$sum:["$$value",{"$cond":{"if":{$eq:["$$this","neutral"]},"then":1,"else":0}}]}}}}}},{$sort:{"topicOccurance":-1}}])