【发布时间】:2018-12-09 05:29:14
【问题描述】:
我有资产集合,其中包含类似的数据
{
"_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"}} }
}
]
)
这样我就得到了重复用户名的结果。
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework