Mongo DB Aggregate

时间:2017-05-24 14:13:44

标签: mongodb aggregation-framework

我有一个包含以下条目的集合:

{"placeId" : "0b980482-ab4b-4685-95ab-ecc2dcba1837","placeName" : "B8 Göteborg","created" : ISODate("2017-04-09T14:53:01.005Z"),"debitAccount" : "1581","debitAmount" : 26.0,"creditAccount" : "3013","creditAmount" : 23.2142791748047,"taxAccount" : "2620","taxAmount" : 2.78572010993958},
{"placeId" : "0b980482-ab4b-4685-95ab-ecc2dcba1837","placeName" : "B8 Göteborg","created" : ISODate("2017-04-09T14:53:01.005Z"),"debitAccount" : "1581","debitAmount" : 26.0,"creditAccount" : "3014","creditAmount" : 53.2142791748047,"taxAccount" : "2621","taxAmount" : 5.78572010993958}

我希望输出类似于:

{
    "_id" : "B8 - Göteborg",
    "days" : [ 
        {
            "date" : "2017-03-24",
            "creditAccounts" : [ 
                {
                    "creditAccount" : "3013",
                    "creditAccountSum" : 23.214279174804
                }, 
                {
                    "creditAccount" : "3014",
                    "creditAccountSum" : 53.2142791748047,
                }, 
            ],
            "debitAccounts" : [ 
                {
                    "debitAccount" : "1581",
                    "debitAccountSum" : 52
                }
            ],
            "taxAccounts" : [ 
                {
                    "taxAccount" : "2620",
                    "taxAccountSum" : 2.78572010993958
                },
                {
                    "taxAccount" : "2621",
                    "taxAccountSum" : 5.78572010993958
                }
            ]
        }
    ]
}

这是每个placeName和day有一个列表,列出所有唯一的creditAccounts,debitAccounts,taxAccounts及其总和。我已经设法为其中一个字段进行分组,它看起来像:

db.getCollection('accounting_records').aggregate([
    {$match : {...},  
    {$group: {_id: {placeName :'$placeName',year: {$substr: ['$created',0,4]},month: {$substr: ['$created',5,2]},dayOfMonth: {$substr: ['$created',8,2]},creditAccount:'$creditAccount'}, created: {$max: '$created'},records: { $addToSet: "$$ROOT" }}},
    {$sort : {created : 1}},
    {$group: {_id: {placeName :'$_id.placeName',year: '$_id.year',month: '$_id.month',dayOfMonth: '$_id.dayOfMonth'},created: {$max: '$created'}, creditAccounts:{$push :{creditAccount: '$_id.creditAccount',sum : {$sum: '$records.creditAmount'}, creditAccountName:'$records.creditAccountName'}}}},
    {$sort : {created : 1}},
    {$group : {_id : '$_id.placeName', created: {$max: '$created'}, days : {$push: {date: {$concat: ['$_id.year','-','$_id.month','-','$_id.dayOfMonth']},creditAccounts : '$creditAccounts'}}}},
    {$sort : {_id : 1}},
])

但我不知道如何设法为所有3个creditAccount,taxAccount&借方账户。也许有可能使用mapReduce但是没有注意到这一点,因为与使用聚合框架相比,我读到的性能要差得多。

1 个答案:

答案 0 :(得分:1)

You need to `$group` each credit/debit/tax-Account/Amount and calculate sum.



db.collection.aggregate([
    {
        $group: {
            _id:{_id:"$placeName",date:{$dateToString:{format:"%Y-%m-%d" , date:"$created"}}, creditAccount:"$creditAccount" },
            creditAccountSum : {$sum: "$creditAmount"},
            remaining: {$push: {debitAccount:"$debitAccount",debitAmount:"$debitAmount",taxAccount:"$taxAccount",taxAmount:"$taxAmount"}}
        }
    },
    {$unwind: "$remaining"},
    {
        $group: {
            _id:{_id:"$_id._id",date:"$_id.date", debitAccount:"$remaining.debitAccount"},
            debitAccountSum : {$sum: "$remaining.debitAmount"},
            remaining: {$push: {creditAccount:"$_id.creditAccount",creditAccountSum:"$creditAccountSum", taxAccount:"$remaining.taxAccount",taxAmount:"$remaining.taxAmount"}}
        }
    },
    {$unwind: "$remaining"},
    {
        $group: {
            _id:{_id:"$_id._id",date:"$_id.date", taxAccount:"$remaining.taxAccount"},
            taxAccountSum : {$sum: "$remaining.taxAmount"},
            remaining: {$push :{creditAccount:"$remaining.creditAccount",creditAccountSum:"$remaining.creditAccountSum", debitAccount:"$_id.debitAccount", debitAccountSum:"$debitAccountSum"}}
        }
    },
    {$unwind: "$remaining"},
    {   
        $group : {
                _id:{_id:"$_id._id",date:"$_id.date"},
                creditAccounts:{$addToSet:{creditAccount:"$remaining.creditAccount",creditAccountSum:"$remaining.creditAccountSum"}},
                debitAccounts:{$addToSet:{debitAccount:"$remaining.debitAccount",debitAccountSum:"$remaining.debitAccountSum"}},
                taxAccounts:{$addToSet:{taxAccount:"$_id.taxAccount",taxAccountSum:"$taxAccountSum"}}      
        }
    },
    {
        $group : {
            _id : {_id:"$_id._id"},
            days : {$push: {date:"$_id.date",creditAccounts:"$creditAccounts",debitAccounts:"$debitAccounts", taxAccounts:"$taxAccounts"}}
        }
    },
    {
        $project: {
            _id:"$_id._id",
            days:"$days"
        }
    }
]).pretty()

测试数据:

{"placeId" : "0b980482-ab4b-4685-95ab-ecc2dcba1837","placeName" : "B8 Göteborg","created" : ISODate("2017-04-09T14:53:01.005Z"),"debitAccount" : "1581","debitAmount" : 26.0,"creditAccount" : "3013","creditAmount" : 23.2142791748047,"taxAccount" : "2620","taxAmount" : 2.78572010993958},
{"placeId" : "0b980482-ab4b-4685-95ab-ecc2dcba1837","placeName" : "B8 Göteborg","created" : ISODate("2017-04-09T14:53:01.005Z"),"debitAccount" : "1581","debitAmount" : 26.0,"creditAccount" : "3014","creditAmount" : 53.2142791748047,"taxAccount" : "2621","taxAmount" : 5.78572010993958},
{"placeId" : "0b980482-ab4b-4685-95ab-ecc2dcba1837","placeName" : "B8 Göteborg","created" : ISODate("2017-05-09T14:53:01.005Z"),"debitAccount" : "1581","debitAmount" : 26.0,"creditAccount" : "3014","creditAmount" : 53.2142791748047,"taxAccount" : "2621","taxAmount" : 5.78572010993958}

输出:

{
    "_id" : "B8 Göteborg",
    "days" : [
        {
            "date" : "2017-04-09",
            "creditAccounts" : [
                {
                    "creditAccount" : "3014",
                    "creditAccountSum" : 53.2142791748047
                },
                {
                    "creditAccount" : "3013",
                    "creditAccountSum" : 23.2142791748047
                }
            ],
            "debitAccounts" : [
                {
                    "debitAccount" : "1581",
                    "debitAccountSum" : 52
                }
            ],
            "taxAccounts" : [
                {
                    "taxAccount" : "2621",
                    "taxAccountSum" : 5.78572010993958
                },
                {
                    "taxAccount" : "2620",
                    "taxAccountSum" : 2.78572010993958
                }
            ]
        },
        {
            "date" : "2017-05-09",
            "creditAccounts" : [
                {
                    "creditAccount" : "3014",
                    "creditAccountSum" : 108.4285583496094
                }
            ],
            "debitAccounts" : [
                {
                    "debitAccount" : "1581",
                    "debitAccountSum" : 52
                }
            ],
            "taxAccounts" : [
                {
                    "taxAccount" : "2621",
                    "taxAccountSum" : 12.57144021987916
                }
            ]
        }
    ]
}