Database

[{"_id":"2020-08","totalContracts":10.0,"totalWarrantyWholesalePrice":109,"totalWarrantyPrice":163.55,"totalSaleAmount":9000,"totalCoverAmount":10000,"totalAmountPaid":47.0,"totalAmountDue":54.55,"balance":7.55,"status":"PARTIAL"},{"_id":"2020-07","totalContracts":1.0,"totalWarrantyWholesalePrice":23,"totalWarrantyPrice":40,"totalSaleAmount":900,"totalCoverAmount":1000,"totalAmountPaid":0,"totalAmountDue":17,"balance":17,"status":"DUE"},{"_id":"2020-06","totalContracts":1.0,"totalWarrantyWholesalePrice":0,"totalWarrantyPrice":0,"totalSaleAmount":900,"totalCoverAmount":1000,"totalAmountPaid":0,"totalAmountDue":0,"balance":0,"status":"PAID"}]

Query

db.collection.aggregate([{$facet:{lifetimeTotals:[{$match:{_id:{$exists:true}}},{$group:{_id:null,totalContracts:{$sum:"$totalContracts"},totalWarrantyWholesalePrice:{$sum:"$totalWarrantyWholesalePrice"},totalWarrantyPrice:{$sum:"$totalWarrantyPrice"},/** Rest of other fields*/}}],months:[{$match:{_id:{$exists:true}}}]}},{$project:{lifetimeTotals:1,monthTotals:{$arrayToObject:{$map:{input:"$months",in:{k:"$$this._id",v:{totalContracts:"$$this.totalContracts",totalWarrantyWholesalePrice:"$$this.totalWarrantyWholesalePrice",totalWarrantyPrice:"$$this.totalWarrantyPrice"/** Rest of other fields*/}}}}}}}])

Result