簡體   English   中英

MongoDB 多組聚合

[英]MongoDB multiple group aggregation

我正在努力獲得我想要的聚合管道的輸出,我很接近但我不確定我需要做什么才能得到我想要的。

我有一個包含銀行對賬單記錄的數據庫,其中有日期、金額、余額方(借方/貸方)和類別(購物/汽車/等)。 我想通過以下方式按年/月/類別分組來聚合這些記錄:

[{
    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>
        }
    }]
}]

現在我的問題是,如何混合此聚合中的類別以獲得我想要的輸出? 我嘗試將類別添加到初始組語句中,但是月數組將包含每個月/類別組合的項目,而不是該月的總和。 任何幫助,將不勝感激!

謝謝!


根據答案更新代碼

[
        { $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"
                }
            }
        }}
    ]

我不知道你的實際字段是什么。 但是您必須使用三個$group階段來實現該輸出。 就像是

db.collection.aggregate([
  { "$group": {
    "_id": { "month": { "$month": "date" }, "year": { "$year": "date" }, "category": "$category" },
    "credit": { "$sum": "$credit" }
    "debit": { "$sum": "$debit" }
  }},
  { "$group": {
    "_id": { "month": { "$month": "$_id.month" }, "year": { "$year": "_id.year" }},
    "categories": {
      "$push": { "category": "$category", "credit": "$credit", "debit": "$debit" }
    },
    "credit": { "$sum": "$credit" }
    "debit": { "$sum": "$debit" }
  }},
  { "$group": {
    "_id": "$_id.year",
    "months": {
      "$push": { "categories": "$categories", "credit": "$credit" "debit": "$debit" }
    }
    "credit": { "$sum": "$credit" }
    "debit": { "$sum": "$debit" }
  }}
])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM