【发布时间】:2021-09-13 20:22:18
【问题描述】:
我正在使用 聚合 查询。 如果出现以下情况,我需要退回文件:
- “总计”不存在
- 数组大小小于“总数”
所以隐藏所有数组等于到“total”的所有文档。
注意事项:
- “容器”可以有不同的“子容器”
- “subContainer”是动态键名
- “总计”是可选的
- “arr”大小
集合中的文档:
[
{ // "container.subContainer2.arr" is lower then "total"
"_id": 1,
"title": "title1",
"container": {
"subContainer1": {
"arr": [1, 2, 3]
},
"subContainer2": {
"arr": [1]
}
},
"total": 3
},
{ // "total" unknow
"_id": 2,
"title": "title2",
"container": {
"subContainer3": {
"arr": [1, 2, 3, 4]
}
}
},
{ // both array size are equal to "total"
"_id": 3,
"title": "title3",
"container": {
"subContainer4": {
"arr": [1, 2, 3, 4, 5]
},
"subContainer5": {
"arr": [1, 2, 3, 4, 5]
}
},
"total": 5
}
]
输出:
[
{
"title": "title1"
},
{
"title": "title2"
}
]
【问题讨论】:
-
什么数组大小应该小于“总”?任何
subContainer或所有subContainer大小? -
@NenadMilosavljevic 大小仅与“arr”数组有关。
-
但是每个
subContainer都有一个arr数组。它们应该都低于total还是至少一个? -
是的,每个
subContainer至少有一个arr数组。它们都可以是 lower 或 equaltotal.
标签: mongodb mongodb-query aggregation-framework