【发布时间】:2021-10-01 16:19:27
【问题描述】:
我有一个包含“父”字段的文档的集合。
[
{
"parent": "P1",
"tagGroups": [],
},
{
"parent": "P1",
"tagGroups": [
{
group: 1,
tags: {
tag1: {
value: true
},
tag2: {
value: "foo"
},
}
},
{
group: 2,
tags: {}
}
]
},
{
"parent": "P2",
"tagGroups": [],
}
]
当至少有一个符合我的条件时,我想请求检索具有相同父级的所有文档:tag1.value = true。
预期:
[
{
"parent": "P1",
"tagGroups": [],
},
{
"parent": "P1",
"tagGroups": [
{
group: 1,
tags: {
tag1: {
value: true
},
tag2: {
value: "foo"
},
}
},
{
group: 2,
tags: {}
}
]
}
]
为此,我想使用 $cond 标记每个文档,然后按父级分组。
https://mongoplayground.net/p/WiIlVeLDrY-
但“if”部分的工作方式似乎与 $match 不同
https://mongoplayground.net/p/_jcoUHE-aOu
您还有其他有效的方法来进行这种查询吗?
编辑:我可以使用查找阶段,但我害怕表现不佳
谢谢
【问题讨论】:
-
请提供一些预期的输入输出
-
嗨,第一个链接显示了我想要做什么,预期的结果是前 2 个文档,而不是没有。
标签: mongodb aggregation-framework