Database
[{_id:1,session_id:2,device_id:3,update_at:ISODate("2022-01-01"),created_at:ISODate("2022-01-01"),event_datum:{_id:1,level:"INFO",priority:0,ts:"2022-07-01T13:37:20",name:"Home",type:"page_load_time",message:"msg",params:[{endPageTime:"2022-07-01",startPageTime:"2022-07-01",total:"41",componentName:"load homepage kpi from storage"}]},std_datum:{}}]
Query
db.collection.aggregate([{/** filter rows by type*/"$match":{"event_datum.type":"page_load_time"}},{/** add a column called firstParam with the first array element of params[]*/$addFields:{firstParam:{$arrayElemAt:["$event_datum.params",0]}}},{/** group rows by message and compute the average using the added field*/$group:{_id:"$event_datum.message",avgLoadTime:{$avg:{"$toInt":"$firstParam.total"}}}},{/** output both columns*/$project:{firstParam:true,avgLoadTime:true}}])