- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个集合,其中的条目如下所示:
{"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/
我有一个 Cassandra 集群,里面有 4 个表和数据。 我想使用聚合函数(sum,max ...)发出请求,但我在这里读到这是不可能的: http://www.datastax.com/docu
我有以下两张表 Table: items ID | TITLE 249 | One 250 | Two 251 | Three 我投票给这些: Table: votes VID | IID | u
这个问题在这里已经有了答案: Update MongoDB field using value of another field (12 个答案) 关闭 3 年前。 我想根据另一个“源”集合的文档中
我的收藏包含以下文件。我想使用聚合来计算里面有多少客户,但我遇到了一些问题。我可以获得总行数,但不能获得总(唯一)客户。 [{ _id: "n001", channel: "Kalip
我有下表 Id Letter 1001 A 1001 H 1001 H 1001 H 1001 B 1001 H 1001 H 1001
得到一列的表 ABC。 “创建”的日期列。所以样本值就像; created 2009-06-18 13:56:00 2009-06-18 12:56:00 2009-06-17 14:02:0
我有一个带有数组字段的集合: {[ name:String buyPrice:Int sellPrice:Int ]} 我试图找到最低和最高买入/卖出价格。在某些条目中,买入或卖出价格为零
我有以下问题: 在我的 mongo db 中,我有以下结构: { "instanceId": "12", "eventId": "0-1b", "activityType":
下面给出的是我要在其上触发聚合查询的 Elasticsearch 文档。 { "id": 1, "attributes": [ { "fieldId": 1,
我正在使用 Django 的 aggregate query expression总计一些值。最终值是一个除法表达式,有时可能以零作为分母。如果是这种情况,我需要一种方法来逃避,以便它只返回 0。 我
我正在学习核心数据,特别是聚合。 当前我想要做的事情:计算表中在某些条件上具有逆关系的多对关系的记录数。 目前我正在这样做: NSExpression *ex = [NSExpression expr
我需要有关 Delphi 中的 ClientDatasets 的一些帮助。 我想要实现的是一个显示客户的网格,其中一列显示每个客户的订单数量。我将 ClientDataset 放在表单上并从 Delp
我的集合有 10M 个文档,并且有一个名为 movieId 的字段;该文档具有以下结构: { "_id" : ObjectId("589bed43e3d78e89bfd9b779"), "us
这个问题已经有答案了: What is the difference between association, aggregation and composition? (21 个回答) 已关闭 9
我在 elasticsearch 中有一些类似于这些示例的文档: { "id": ">", "list": [ "a", "b", "c" ] } { "id"
我正在做一些聚合。但是结果完全不是我所期望的,似乎它们没有聚合索引中与我的查询匹配的所有文档,在这种情况下 - 它有什么好处? 例如,首先我做这个查询: {"index":"datalayer","t
假设我在 ES 中有这些数据。 | KEY | value | |:-----------|------------:| | A |
可能在我的文档中,我有一个被分析的文本字段。我只是在ElasticSearch AggregationAPI中迷路了。我需要2种不同情况的支持: 情况A)结果是带有计数标记(条款)的篮子下降。 情况B
我正在为网上商店构建多面过滤功能,如下所示: Filter on Brand: [ ] LG (10) [ ] Apple (5) [ ] HTC (3) Filter on OS: [ ] Andr
我有一个父/子关系并且正在搜索 child 。 是否可以在父属性上创建聚合? 例如parent 是 POST,children 是 COMMENT。如果父项具有“类别”属性,是否可以搜索 COMMEN
我是一名优秀的程序员,十分优秀!