Database

[{"_id":ObjectId("61f512fd138a7bf92ec0fd3c"),"agent_id":"05046630001","c_id":null,"agentName":"Testing","agent_intercom_id":"4554","campaign":["Campaig227"],"first_login_time":ISODate("2021-12-28T11:55:00Z"),"last_logout_time":ISODate("2022-01-21T03:20:10Z"),"parent_id":4663.0,"total_call":2.0,"outbound_call":1.0,"iinbound_call":1.0,"average_call_handling_time":56.0,"logged_in_duration":2.0,"total_in_call_time":30.0,"total_break_duration":10.0,"total_ring_time":2.0,"available_time":40.0,"ideal_time":0.0,"occupancy":0.0,"inbound_calls_missed":0.0,"created_at":ISODate("2021-11-29T19:30:00Z")}]

Query

db.collection.aggregate([{$group:{_id:{agent_id:"$agent_id",campaign:"$campaign",date:{$dateTrunc:{date:"$created_at",unit:"week",timezone:"Europe/Zurich",startOfWeek:"monday"}}},"Total call":{$sum:"$total_in_call_time"},Outgoing:{$sum:"$outbound_call"},Incoming:{$sum:"$iinbound_call"},"Average Call":{$avg:"$total_in_call_time"},"Total Time":{$sum:"$total_call"},"Idle Time":{$sum:"$ideal_time"}}},{$set:{"Average Call":{$dateToString:{date:{$toDate:{$multiply:["$Average Call",1000]}},format:"%H:%M:%S"}},"Total Time":{$dateToString:{date:{$toDate:{$multiply:["$Total Time",1000]}},format:"%H:%M:%S"}},"Idle Time":{$dateToString:{date:{$toDate:{$multiply:["$Idle Time",1000]}},format:"%H:%M:%S"}}}},{$replaceWith:{$mergeObjects:["$_id","$$ROOT"]}},{$unset:"_id"}])

Result