db.test3.find()
{ "_id" : 1, "results" : [{"result" : {"cost" : [ { "priceAmt" : 100 } ] } } ] }
I tried the following unsucessfully:
db.test3.aggregate({$group : {_id: "", total : {$sum:
$results.result.cost.priceAmt"}}}, {$project: {_id: 0, total: 1}})
{ "result" : [ { "total" : 0 } ], "ok" : 1 }
EDIT
Desired output:
100 // sum of each "priceAmt"
You'll have to use the $unwind operator to turn array items into individual documents.
db.test3.aggregate({$unwind: "$results"}, {$unwind: "$results.result.cost"}, {$group : {_id: "", total : {$sum: "$results.result.cost.priceAmt"}}}, {$project: {_id: 0, total: 1}})
The $unwind needs to be applied twice because you have a nested array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With