Mongo Playground

Configuration

[{"collection":"collection","class":10,"age":20,},{"collection":"collection","class":9,"age":20,}]

Query

db.collection.aggregate([{$group:{_id:"$class",total:{$sum:"$age"}}}])

Result