描述: 我的数据来自mongodb这样的项目:
{ "_id" : "5620150715", "timeStamp" : NumberLong("1436889600000"), "userId" : 56, "WorldAmounts" : [ { "count" : NumberLong(10), "Address" : "aaa" }, { "count" : NumberLong(20), "Address" : "bbb" } ], "Amounts" : [ { "count" : NumberLong(10), "ipAddress" : "aaa" }, { "count" : NumberLong(20), "Address" : "bbb" } ], "date" : "20150715", "localAmounts" : [ ] }
问题:
如何按所有项目分组,关键是" userId
"和#34; Address
"在WorldAmounts
?
这样的测试数据:
[{'userId':1, 'localA': [{'address':'A','count':7},{'address':'B', 'count':10}], 'localB': [{'address':'X','count':5}], 'localALL':[{'address':'A','count':7},{'address':'B', 'count':10},{'address':'X','count':5}]},{'userId':2, 'localA': [{'address':'A','count':7},{'address':'B', 'count':10}], 'localB': [{'address':'X','count':5}], 'localALL':[{'address':'A','count':7},{'address':'B', 'count':10},{'address':'X','count':5}]},{'userId':1, 'localA': [{'address':'C','count':7}], 'localB': [{'address':'X','count':5}], 'localALL':[{'address':'A','count':7},{'address':'B', 'count':10},{'address':'X','count':5}]}]
结果如下:
如果' userId
'如果不使用groupby,则localALL
为LocalA + LocalB
,如果相同' address
'价值,' count
'的总和否则附加本地
[{'userId':1, 'localA': [{'address':'C','count':7},{'address':'A','count':7},{'address':'B', 'count':10}], 'localB': [{'address':'X','count':10}], 'localALL':[{'address':'A','count':7},{'address':'B', 'count':10},{'address':'X','count':10}]},
{'userId':2, 'localA': [{'address':'A','count':7},{'address':'B', 'count':10}], 'localB': [{'address':'X','count':5}], 'localALL':[{'address':'A','count':7},{'address':'B', 'count':10},{'address':'X','count':5}]}
]
alxasin建议这样一个最简单的例子:
输入: [{' abs':1,' lad':[{' count':2,' desc':' A&# 39;},{'计数':3,'降序':4}]}, {' abs':1,' lad':[{' count':3,' desc':' A' ;},{'计数':1,'降序':5}]}]
输出:
[{' abs':1,' lst':[{' count':5,' desc':3}, {' count':3,' desc':4},{' count':1,' desc':5}]}]