Database

[{"_id":ObjectId("5fd3e6a227e9bc610e4daec3"),"completed":{"value":true,"dateCompleted":ISODate("2020-12-11T23:37:38.544Z")},"productType":"car","instructionType":"new","company":"company","client":{"_id":ObjectId("5fd3db7427e9bc610e4daeaa"),"firstName":"test","lastName":"client"},"createdAt":ISODate("2020-12-11T21:37:38.544Z"),"updatedAt":ISODate("2021-01-11T19:34:56.486Z"),},{"_id":ObjectId("5fd3e6a227e9bc610e4daec4"),"completed":{"value":true,"dateCompleted":ISODate("2020-12-11T23:37:38.544Z")},"productType":"car","instructionType":"new","company":"company","client":{"_id":ObjectId("5fd3db7427e9bc610e4daeab"),"firstName":"another","lastName":"one"},"createdAt":ISODate("2020-12-10T21:37:38.544Z"),"updatedAt":ISODate("2021-01-11T19:34:56.486Z"),}]

Query

db.collection.aggregate([{"$facet":{"total":[{$match:{company:"company"}},{$count:"total"}],"mostPopularProduct":[{$match:{company:"company"}},{$sortByCount:"$productType"}],"instructions":[{$match:{company:"company"}},{$group:{_id:"$client._id",firstName:{$first:"$client.firstName"},lastName:{$last:"$client.lastName"},totalInstructions:{$sum:1},completedInstructions:{$sum:{"$cond":[{"$eq":["$completed.value",true]},1,0]}},avgResolutionTime:{"$avg":{"$cond":[{"$eq":["$completed.value",true]},{"$subtract":[{"$ifNull":["$completed.dateCompleted",0]},{"$ifNull":["$createdAt",0]}]},null]}},}},]}},{"$project":{"totals":{"totalInstructions":{$arrayElemAt:["$total.total",0]},"mostPopularProduct":"$mostPopularProduct",},"perClient":{"instructions":"$instructions"}}}])

Result