【发布时间】:2021-06-20 22:05:34
【问题描述】:
我的 MongoDB 数据库中有以下文档的结构:
[
{
name: "x1",
stages: [
{
_id: "60cb49c4977b0c64f2667054",
type: "y1"
},
{
_id: "60cb49c4977b0c64f2667055",
type: "y2"
},
...
]
},
{
name: "x2",
stages: [
{
_id: "60cb49c4977b0c64f2667054",
type: "y1"
},
{
_id: "60cb49c4977b0c64f2667055",
type: "y2"
},
...
]
},
]
我想在每个主文档(name 的 x1,x2,... 的那些)和 stages 按他们的类型(y1,y2,...)分组,所以基本上是嵌套的组操作。
这是我希望使用聚合方法得到的预期结果:
[
{
name: "x1",
stages: [
{
type: "y1",
stages: [
{
_id: "60cb49c4977b0c64f2667054",
type: "y1"
}
]
},
{
type: "y2",
stages: [
{
_id: "60cb49c4977b0c64f2667055",
type: "y2"
}
]
}
]
},
{
name: "x2",
stages: [
{
type: "y1",
stages: [
{
_id: "60cb49c4977b0c64f2gggg667054",
type: "y1"
}
]
},
{
type: "y2",
stages: [
{
_id: "60cb49c4977b0c64f2tttt667055",
type: "y2"
}
]
}
]
}
]
有什么帮助吗?我不知道如何继续我的聚合方法(这个快照代码是我成功编写预代码聚合的格式)
【问题讨论】:
-
您认为提供的答案中是否有某些内容无法解决您的问题?如果是这样,那么请对答案发表评论,以阐明究竟需要解决哪些尚未解决的问题。
标签: mongodb aggregation-framework