'Aggregation: How would I count the number of keys for which the value is true?

I have a load of documents in mongodb which look something like this:

[
  {
    name: "steve",
    accountFeatures: {word: true, excel: false, powerpoint: true}
  },
  {
    name: "john",
    accountFeatures: {word: false, excel: true, powerpoint: true, notes: true}
  },
  {
    name: "rick",
    accountFeatures: {word: true, excel: false, powerpoint: true}
  }
]

I want to run an aggregation to find out how many of each of the keys in the objects are set to true (there are many more keys in the actual data), so the expected output for the sample data would be:

  {
    "res": {
      "excel": 1,
      "notes": 1,
      "powerpoint": 3,
      "word": 2
    }
  }

Is this possible? So far all I can think to do is this:

[
  {
    '$project': {
      '_id': 0, 
      'accountFeatures': 1
  }
]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source