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:{topic:"$comment.topic",text_sentiment:"$comment.text_sentiment"},total:{$sum:1},}},{$group:{_id:"$_id.topic",total:{$sum:1},text_sentiments:{$push:{k:"$_id.text_sentiment",v:"$total"}}}},{$project:{topic:"$_id",topicOccurance:"$total",sentiment:{"$arrayToObject":"$text_sentiments"}}},{$sort:{"topicOccurance":-1}}])

Result