跨数组元素求和

时间:2019-03-24 20:47:04

标签: arrays mongodb

我有以下文件:

{
"month": 2,
"year": 2019,
"categories": [
    {
        "title": "Income",
        "budget": 5000,
        "amount": 2000,
        "subcategories": [
            {
                "title": "Paychecks",
                "budget": 4000,
                "amount": 1000,
                "transactions": [
                    {
                        "day": 3,
                        "amount": 600,
                        "vendor": "Microsoft",
                        "memo": "Paycheck 1"
                    },
                    {
                        "day": 10,
                        "amount": 400,
                        "vendor": "Google",
                        "memo": "Paycheck 2"
                    }
                ]
            },
            {
                "title": "Other",
                "budget": 1000,
                "amount": 1000,
                "transactions": [
                    {
                        "day": 7,
                        "amount": 600,
                        "vendor": "Mom",
                        "memo": "Gift for Baby"
                    },
                    {
                        "day": 19,
                        "amount": 400,
                        "vendor": "Amy",
                        "memo": "Gift for Julie"
                    }
                ]
            }
        ]
    },
    {
        "title": "Housing",
        "budget": 3000,
        "amount": 3000,
        "subcategories": [
            {
                "title": "Rent",
                "budget": 2000,
                "amount": 2000,
                "transactions": [
                    {
                        "day": 1,
                        "amount": 2000,
                        "vendor": "Homes4Rent",
                        "memo": "Rent"
                    }
                ]
            },
            {
                "title": "Electric",
                "budget": 1000,
                "amount": 1000,
                "transactions": [
                    {
                        "day": 14,
                        "amount": 1000,
                        "vendor": "Tampa Electric",
                        "memo": "Electric"
                    }
                ]
            }
        ]
    }
]}

我需要确保“ categories.budget”金额始终等于该类别的“ categories.subcategories.budget”金额之和。

例如,我需要确保“收入”的预算为5000,因为其子类别(“支票”和“其他”)的预算总和为5000。

最理想的解决方案是可以在单个查询中执行总和和“ categories.budget”字段更新的解决方案,但是我不确定如何完成此操作,甚至不确定是否可行。到目前为止,这是我想出的:

db.collection.aggregate(
[
    {
        $match: {
            month:2,
            year:2019
        }
    },
    {
        $unwind: "$categories"
    },
    {
        $unwind: "$categories.subcategories"
    },
    {
        $group: {
            _id: {
                category: "$categories.title"
            },
            sum: {
                $sum: "$categories.subcategories.budget"
            }
        }
    }
])

不幸的是,这似乎只是在打印类别预算,而不是对它们进行汇总。无论实现哪种解决方案,最终都将在我的应用程序的各个地方使用它来执行类似的操作。

非常感谢,让我知道是否需要提供更多信息。

0 个答案:

没有答案