How to sum and count in 3rd level nested array

Hi @Nuur_zakki_Zamani

Its fairly an easier Pipeline :

db.collection.aggregate([
  {
    "$match": {
      "companyId": ObjectId("635c70892e8cfaf4a7d49a3f")
    }
  },
  {
    "$unwind": {
      "path": "$datas"
    }
  },
  {
    "$unwind": {
      "path": "$datas.report"
    }
  },
  {
    "$unwind": {
      "path": "$datas.report.scores"
    }
  },
  {
    "$group": {
      _id: "$datas.report.scores.key",
      sum: {
        $sum: "$datas.report.scores.value"
      }
    }
  }
])

Thanks
Pavel

1 Like