Besides its other features MongoDB also support aggreagtions within the database. One way is to use the built-in aggreagtion framework and the other map-reduce.
From my point of view the aggregation frameworks works good for simple aggregations but for complicated stuff like summing up and also average calcluations map-reduce is the better approach.
The only thing that went wrong in the beginning is that map and reduce have to return the same data because reducing is only a step for summing up all the stuff. Average calculation has to be done in a final step, called finalize.
The output is written to a collection.
See the examples here.
As a performance indicator I can say that it aggregates 4 mio documents (summing up some internal structures and taking averages on main categories and overall) in about 5 minutes and was 4 hours with pure Java implementation before.