我有资产收集,其中包含数据
{
"_id" : ObjectId("5bfb962ee2a301554915"),
"users" : [
"abc.abc@abc.com",
"abc.xyz@xyz.com"
],
"remote" : {
"source" : "dropbox",
"bytes" : 1234
}
{
"_id" : ObjectId("5bfb962ee2a301554915"),
"users" : [
"pqr.pqr@pqr.com",
],
"remote" : {
"source" : "google_drive",
"bytes" : 785
}
{
"_id" : ObjectId("5bfb962ee2a301554915"),
"users" : [
"abc.abc@abc.com",
"abc.xyz@xyz.com"
],
"remote" : {
"source" : "gmail",
"bytes" : 5647
}
我正在寻找的是用户分组,并根据其来源获得字节总数
{
"_id" : "abc.abc@abc.com",
"bytes" : {
"google_drive": 1458,
"dropbox" : 1254
}
}
我没有得到如何使用分组获得嵌套输出 . 我尝试过查询
db.asset.aggregate(
[
{$unwind : '$users'},
{$group:{
_id:
{'username': "$users",
'source': "$remote.source",
'total': {$sum: "$remote.bytes"}} }
}
]
)
这样我就可以用重复的用户名获得结果 .