我正在努力获取所需的聚合管道的输出,我已经接近了,但是我不确定要获得所需的内容需要做什么。
我有一个包含银行对帐单记录的数据库,其中包含日期,金额,余额(借方/贷方)和类别(购物/汽车/等)。我想通过以下方式将它们按年/月/类别分组来汇总这些记录:
[{
credit: <total sum of year>,
debit: <total sum of year>,
_id: <year>
months: [{
month: <month number>,
credit: <total sum of month>,
debit: <total sum of month>,
categories: [{
category: <category id>,
credit: <total sum of category in that month>,
debit: <total sum of category in that month>
}]
}]
}]
我目前有以下汇总声明:
[
{ $group: {
_id: { maand: { $month: "$datum"}, jaar: { $year: "$datum"}}, //categorie: "$categorie",
debet: {
$sum: {
$switch: {
"branches": [
{
"case": {$eq: ["$zijde", "D"]},
"then": "$bedrag"
}
],
"default": 0
}
}
},
credit: {
$sum: {
$switch: {
"branches": [
{
"case": {$eq: ["$zijde", "C"]},
"then": "$bedrag"
}
],
"default": 0
}
}
}
}},
{ $group: {
_id: "$_id.jaar",
debet: {$sum: "$debet"},
credit: {$sum: "$credit"},
maanden: {
$push: {
maand: "$_id.maand",
debet: "$debet",
credit: "$credit"
}
}
}}
],
哪一个产生了我想要的,但还不包括类别。这是输出:
[{
credit: ***,
debit: ***,
_id: <year>
months: [{
{
credit: ***,
debit: ***,
month: <month number>
}
}]
}]
现在我的问题是,如何在此聚合中混合类别以获得所需的输出?我尝试将类别添加到初始组语句中,但是,months数组将包含每个月/类别组合的项目,而不再是该月的总和。任何帮助将不胜感激!
谢谢!
基于答案的更新代码
[
{ $group: {
_id: { maand: { $month: "$datum"}, jaar: { $year: "$datum"}, categorie: "$categorie"}, //categorie: "$categorie",
debet: {
$sum: {
$switch: {
"branches": [
{
"case": {$eq: ["$zijde", "D"]},
"then": "$bedrag"
}
],
"default": 0
}
}
},
credit: {
$sum: {
$switch: {
"branches": [
{
"case": {$eq: ["$zijde", "C"]},
"then": "$bedrag"
}
],
"default": 0
}
}
}
}},
{ $group: {
_id: {maand: "$_id.maand", jaar: "$_id.jaar"},
categories: {
$push: {
categorie: "$_id.categorie",
debet: "$debet",
credit: "$credit"
}
},
debet: {$sum: "$debet"},
credit: {$sum: "$credit"},
}},
{ $group: {
_id: "$_id.jaar",
debet: {$sum: "$debet"},
credit: {$sum: "$credit"},
maanden: {
$push: {
maand: "$_id.maand",
categories: "$categories",
debet: "$debet",
credit: "$credit"
}
}
}}
]
答案 0 :(得分:1)
我不知道你是什么人
实际领域是。但是您必须使用三个Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Processing Data')));
阶段才能实现该输出。像
AppBar