mongodb - 拆分为桶,并在每个桶上运行聚合
问题描述
我有一个聚合查询,它将我的数据分成桶。我想知道是否可以选择更多的聚合管道阶段,这些阶段将分别在每个存储桶上运行。
这是我的数据:
[
{
"entityId":1,
"accountId":"X",
"date":"2019-10-10"
},
{
"entityId":2,
"accountId":"X",
"date":"2019-10-10"
},
{
"entityId":3,
"accountId":"Y",
"date":"2019-10-10"
},
{
"entityId":4,
"accountId":"Y",
"date":"2019-10-10"
}
]
我正在将我的数据拆分为 accountId 存储桶,现在它看起来像这样 -
[
{
"accountId":"X",
"results":[
{
"entityId":1,
"date":"2019-10-10"
}
]
},
{
"accountId":"Y",
"results":[
{
"entityId":3,
"date":"2019-10-10"
}
]
}
]
我想分别在每个桶上继续聚合管道,并按“日期”对其余数据进行分组,所以它看起来像这样:
[
{
"accountId":"X",
"results":[
{
"date":"2019-10-10",
"results":[
{
"entityId":1
},
{
"entityId":2
}
]
}
]
},
{
"accountId":"Y",
"results":[
{
"date":"2019-10-10",
"results":[
{
"entityId":3
},
{
"entityId":4
}
]
}
]
}
]
我试图了解如何在先前 $bucket 的结果上使用 $bucket 步骤,但我遇到了麻烦..
提前致谢!
解决方案
推荐阅读
- java - 在覆盖 toString 后在类中使用自定义打印方法已经完成
- tensorflow - tf-agent 的 `policy` 和 `collect_policy` 有什么区别?
- sql - 覆盖表行中存在的数据的有效方法
- list - Flutter 列表构建
- python - 有人可以解释这两个情节的工作原理吗?
- javascript - 使用javascript过滤特定时间戳之间的数据
- r - 根据我在 R 中的数据分布生成新的随机分布
- kubernetes - kubectl run 命令创建多个 pod 而不是一个
- python - 不在 GoogleNews-vectors-negative300.bin 词汇表中的词
- c++ - c++中静态类的优缺点