You should take a look at https://docs.mongodb.com/manual/aggregation/. The $match and $group stages would be a good starting point.
MongoDB university offers a an aggregation framework specific course (M121). You may find more information at https://university.mongodb.com/.
But you can do it in JavaScript also using any JavaScript arithmetic operators.