1) Data ready
var a = {"name": "Zhekou", "chardate": "2015-12-01", "Detail":
[{"Detailid": 1, "Abstract": "1a", "Lenderamt": $, "Debtamt": 0, "Baseamt": 20},
{"Detailid": 2, "Abstract": "2a", "Lenderamt": 3, "Debtamt": 0, "Baseamt": 3},
{"Detailid": 3, "Abstract": "3a", "Lenderamt": 4, "Debtamt": 0, "Baseamt": 4}]
}
Db. Voucher.insert (a);
var a = {"name": "Zhekou", "chardate": "2015-12-01", "Detail":
[{"Detailid": 4, "Abstract": "1a", "Lenderamt":, "Debtamt": 0, "Baseamt": 56},
{"Detailid": 5, "Abstract": "2a", "Lenderamt":, "Debtamt": 0, "Baseamt": 34},
{"Detailid": 6, "Abstract": "3a", "Lenderamt": "Debtamt": 0, "Baseamt": 87}]
}
Db. Voucher.insert (a);
2) function
Db. Voucher.aggregate (
{$unwind: "$Detail"},
{$match: {"detail.abstract":/a/}},
{$project: {_id:0, Amount: "$Detail. Baseamt", Abstract: "$Detail. Abstract"}},
{$group: {_id: ' $Abstract ', total:{$sum: ' $amount '}}}
)
2_1)
$match: Filter and then group, Detail.abstract, although with unwind open, but still want to use Detail. Prefix reference
$project: Renaming
$: Reference
$group: Grouping, _id: Grouping by**, $sum: adding
3) Result
/* 0 */
{
"Result": [
{
"_id": "3a",
"Total": 91
},
{
"_id": "2a",
"Total": 37
},
{
"_id": "1a",
"Total": 76
}
],
"OK": 1
}
Test Water MongoDB ER