Database

[{"_id":ObjectId("5ef3b94d617c89736b7f4967"),"action":"started","process":"combined.js","name":"Product 1","location":{"branchId":"1"},},{"_id":ObjectId("5ef3b9850263df74c16a0819"),"args":["102","false","06-23-2020","true"],"action":"processing","process":"combined.js","name":"Product 2","location":{"branchId":"1"},},{"_id":ObjectId("5ef3b9b3e9109c75e19ead0e"),"action":"errored","process":"combined.js","name":"Product 2","location":{"branchId":"2"},}]

Query

db.collection.aggregate([{$match:{}},{$group:{_id:"$location.branchId",data:{$push:"$$ROOT"}}},{$replaceWith:{$arrayToObject:[{$concatArrays:[[{"k":"_id",v:"$_id"}],{$map:{input:"$data",in:{k:"$$this.name",v:{started:{$eq:["$$this.action","started"]},processing:{$eq:["$$this.action","processing"]},errored:{$eq:["$$this.action","errored"]},completed:{$eq:["$$this.action","completed"]}}}}}]}]}}])

Result