Database

[{"_id":ObjectId("5e71a1f3081c4b70cdbc438f"),"DataSetID":ObjectId("5e71a1f3081c4b70cdbc438e"),"row":[{"key":"Region","prev":"root","value":"Australia and Oceania","typeOfValue":"string","currentDepth":1},{"key":"Country","prev":"root","value":"Tuvalu","typeOfValue":"string","currentDepth":1},{"key":"Item Type","prev":"root","value":"Baby Food","typeOfValue":"string","currentDepth":1},{"key":"Sales Channel","prev":"root","value":"Offline","typeOfValue":"string","currentDepth":1},{"key":"Order Priority","prev":"root","value":"H","typeOfValue":"string","currentDepth":1},{"key":"Order Date","prev":"root","value":ISODate("2010-05-27T18:30:00.000Z"),"typeOfValue":"date","currentDepth":1},{"key":"Order ID","prev":"root","value":669165933,"typeOfValue":"number","currentDepth":1},{"key":"Ship Date","prev":"root","value":ISODate("2010-06-26T18:30:00.000Z"),"typeOfValue":"date","currentDepth":1},{"key":"Units Sold","prev":"root","value":9925,"typeOfValue":"number","currentDepth":1},{"key":"Unit Price","prev":"root","value":255.28,"typeOfValue":"number","currentDepth":1},{"key":"Unit Cost","prev":"root","value":159.42,"typeOfValue":"number","currentDepth":1},{"key":"Total Revenue","prev":"root","value":2533654,"typeOfValue":"number","currentDepth":1},{"key":"Total Cost","prev":"root","value":1582243.5,"typeOfValue":"number","currentDepth":1},{"key":"Total Profit","prev":"root","value":951410.5,"typeOfValue":"number","currentDepth":1}]},{"_id":ObjectId("5e71a1f3081c4b70cdbc4381"),"DataSetID":ObjectId("5e71a1f3081c4b70cdbc438f"),"row":[{"key":"Region","prev":"root","value":"Australia and Oceania","typeOfValue":"string","currentDepth":1},{"key":"Country","prev":"root","value":"India","typeOfValue":"string","currentDepth":1},{"key":"Item Type","prev":"root","value":"Baby Food","typeOfValue":"string","currentDepth":1},{"key":"Sales Channel","prev":"root","value":"Offline","typeOfValue":"string","currentDepth":1},{"key":"Order Priority","prev":"root","value":"H","typeOfValue":"string","currentDepth":1},{"key":"Order Date","prev":"root","value":ISODate("2010-05-27T18:30:00.000Z"),"typeOfValue":"date","currentDepth":1},{"key":"Order ID","prev":"root","value":669165933,"typeOfValue":"number","currentDepth":1},{"key":"Ship Date","prev":"root","value":ISODate("2010-06-26T18:30:00.000Z"),"typeOfValue":"date","currentDepth":1},{"key":"Units Sold","prev":"root","value":9925,"typeOfValue":"number","currentDepth":1},{"key":"Unit Price","prev":"root","value":255.28,"typeOfValue":"number","currentDepth":1},{"key":"Unit Cost","prev":"root","value":159.42,"typeOfValue":"number","currentDepth":1},{"key":"Total Revenue","prev":"root","value":2533654,"typeOfValue":"number","currentDepth":1},{"key":"Total Cost","prev":"root","value":1582243.5,"typeOfValue":"number","currentDepth":1},{"key":"Total Profit","prev":"root","value":10.5,"typeOfValue":"number","currentDepth":1}]},{"_id":ObjectId("5e71a1f3081c4b70cdbc4380"),"DataSetID":ObjectId("5e71a1f3081c4b70cdbc4389"),"row":[{"key":"Region","prev":"root","value":"Australia and Oceania","typeOfValue":"string","currentDepth":1},{"key":"Item Type","prev":"root","value":"Baby Food","typeOfValue":"string","currentDepth":1},{"key":"Sales Channel","prev":"root","value":"Offline","typeOfValue":"string","currentDepth":1},{"key":"Order Priority","prev":"root","value":"H","typeOfValue":"string","currentDepth":1},{"key":"Order Date","prev":"root","value":ISODate("2010-05-27T18:30:00.000Z"),"typeOfValue":"date","currentDepth":1},{"key":"Order ID","prev":"root","value":669165933,"typeOfValue":"number","currentDepth":1},{"key":"Ship Date","prev":"root","value":ISODate("2010-06-26T18:30:00.000Z"),"typeOfValue":"date","currentDepth":1},{"key":"Units Sold","prev":"root","value":9925,"typeOfValue":"number","currentDepth":1},{"key":"Unit Price","prev":"root","value":255.28,"typeOfValue":"number","currentDepth":1},{"key":"Unit Cost","prev":"root","value":159.42,"typeOfValue":"number","currentDepth":1},{"key":"Total Revenue","prev":"root","value":2533654,"typeOfValue":"number","currentDepth":1},{"key":"Total Cost","prev":"root","value":1582243.5,"typeOfValue":"number","currentDepth":1},{"key":"Total Profit","prev":"root","value":951410.5,"typeOfValue":"number","currentDepth":1}]},{"_id":ObjectId("5e71a1f3081c4b70cdbc431f"),"DataSetID":ObjectId("5e71a1f3081c4b70cdbc431e"),"row":[{"key":"Region","prev":"root","value":"Australia and Oceania","typeOfValue":"string","currentDepth":1},{"key":"Country","prev":"root","value":"Tuvalu","typeOfValue":"string","currentDepth":1},{"key":"Item Type","prev":"root","value":"Baby Food","typeOfValue":"string","currentDepth":1},{"key":"Sales Channel","prev":"root","value":"Offline","typeOfValue":"string","currentDepth":1},{"key":"Order Priority","prev":"root","value":"H","typeOfValue":"string","currentDepth":1},{"key":"Order Date","prev":"root","value":ISODate("2010-05-27T18:30:00.000Z"),"typeOfValue":"date","currentDepth":1},{"key":"Order ID","prev":"root","value":669165933,"typeOfValue":"number","currentDepth":1},{"key":"Ship Date","prev":"root","value":ISODate("2010-06-26T18:30:00.000Z"),"typeOfValue":"date","currentDepth":1},{"key":"Units Sold","prev":"root","value":9925,"typeOfValue":"number","currentDepth":1},{"key":"Unit Price","prev":"root","value":255.28,"typeOfValue":"number","currentDepth":1},{"key":"Unit Cost","prev":"root","value":159.42,"typeOfValue":"number","currentDepth":1},{"key":"Total Revenue","prev":"root","value":2533654,"typeOfValue":"number","currentDepth":1},{"key":"Total Cost","prev":"root","value":1582243.5,"typeOfValue":"number","currentDepth":1},{"key":"Total Profit","prev":"root","value":1.5,"typeOfValue":"number","currentDepth":1}]}]

Query

db.collection.aggregate([{$match:{"row.key":"Country"}},{$project:{_id:0,row:{$reduce:{input:"$row",initialValue:{country:"",totalProfit:0},in:{country:{$cond:[{$eq:["$$this.key","Country"]},"$$this.value","$$value.country"]},totalProfit:{$cond:[{$eq:["$$this.key","Total Profit"]},"$$this.value","$$value.totalProfit"]}}}}}},{$group:{_id:"$row.country",value:{$sum:"$row.totalProfit"}}}])

Result