我正在尝试在一系列信贷和债务中求和,但结果却是错误的(信用:800,借方:8)是400和4。我不知道我在做什么错。
我的收藏
{
"_id": "5ca2156960e3fa38a0087cd8",
"name": "Jan",
"data": "2019-01-01T02:00:00.000Z",
"__v": 18,
"credits": [
{
"_id": "5ca24eabbc5e01341c034ecf",
"name": "cred 1",
"valor": 200
},
{
"_id": "5ca24ea9bc5e01341c034ece",
"name": "cred 2",
"valor": 200
},
],
"debits": [
{
"_id": "5ca230cffc553e36ec5841dd",
"name": "debit1",
"value": 2,
"status": "OK"
},
{
"_id": "5ca230cafc553e36ec5841dc",
"name": "debit2",
"value": 2,
"status": "OK"
}] }
代码:
db.collection.aggregate([
{ $unwind: '$credits' },
{ $unwind: '$debits' },
{$project: {
credits: { $sum: '$credits.value' },
debits: { $sum: '$debits.value' }}},
{
$group: {
_id: null,
credits: { $sum: '$credit' },
debits: { $sum: '$debits' }
}},
{$project: {
id: 0,
credits: 1,
debits: 1
}}])