How to count the number of certain elements in an array with objects?

I have an array with objects with operations.
I need to count the number of all operations and display them in a separate object.
Thank you in advance!

Array with objects with operations

  "operationsHistory": [
    {
      "operationName": "buying",
      "amount": 5,
    },
    {
      "operationName": "deleting",
    },
    {
      "operationName": "buying",
      "amount": 5,
    },
    {
      "operationName": "buying",
      "amount": 2,
    },
    {
      "operationName": "deleting",
    },
    {
      "operationName": "deleting",
    },
    {
      "operationName": "buying",
      "amount": 5,
    },
    {
      "operationName": "deleting",
    },
    {
      "operationName": "buying",
      "amount": 3,
    }
  ]

What do I want to get

  "operationsCount": {
      "buying": 5,
      "selling": 0,
      "deleting": 4
  },

You simply $unwind the array operationsHistory, then $group: by _id:operationName using the $sum accumulator.

use this below MQL to do the above request

db.sample.aggregate()
.unwind("$operationsHistory")
.project({amount: "$operationsHistory.amount", operationName: "$operationsHistory.operationName"})
.group({ _id: "$operationName", count: {$sum: "$amount"}})