How to turn an array into an object with summed values?

Hello, welcome : )

If you want the sums in 1 document you can try something like this

Query

  • group by pool and count
  • collect those documents in an array in key/value pairs
  • array to object(with parent traits) and replace root with it

Playmongo (put the cursor in the end of each stage to see what it does)

aggregate(
[{"$group": {"_id": "$pool", "count": {"$sum": "$successes"}}},
 {"$group": {"_id": null, "docs": {"$push": {"k": "$_id", "v": "$count"}}}},
 {"$replaceRoot": {"newRoot": {"traits": {"$arrayToObject": ["$docs"]}}}}])

This looks like the expected ouput if this is not what you need send if you can the expected output.