gpt4 book ai didi

mongodb - MongoDB 聚合

转载 作者:可可西里 更新时间:2023-11-01 09:48:20 27 4
gpt4 key购买 nike

我有一个集合,其中的条目如下所示:

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

每个地点名称和日期都有一个包含所有唯一的 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 和 debitAccount 进行分组。也许使用 mapReduce 是可能的,但没有考虑到这一点,因为我读过与使用聚合框架相比性能要差得多。

最佳答案

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

关于mongodb - MongoDB 聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44160970/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com