Database

[{Category:"A",Group:1},{Category:"A",Group:1},{Category:"A",Group:2},{Category:"A",Group:3},{Category:"B",Group:5},{Category:"B",Group:5},]

Query

db.collection.aggregate([{"$group":{"_id":"$Category","groups":{"$addToSet":"$Group"}}},{"$replaceWith":{"$arrayToObject":[[{"k":{"$concat":["Category ","$_id"]},"v":{"$size":"$groups"}}]]}}])

Result