node.js - MongoDB:如何使用条件递归获取所有子节点的计数?
问题描述
如何使用条件递归获取所有子节点的计数?
我有这样一个集合的结构:
| id | parentID | type |
-----------------------------
| 1 | NULL | A |
-----------------------------
| 2 | 1 | B |
-----------------------------
| 3 | 1 | B |
-----------------------------
| 4 | 2 | C |
-----------------------------
| 5 | 2 | C |
-----------------------------
| 6 | 2 | B |
-----------------------------
并想要这样的输出:
[
{
"id": 1,
"parentId": null,
"type": "A",
"childBCount": 3
},
{
"id": 2,
"parentId": 1,
"type": "B",
"childBCount": 1
},
{
"id": 3,
"parentId": 1,
"type": "B",
"childBCount": 0
},
{
"id": 4,
"parentId": 2,
"type": "C",
"childBCount": 0
},
{
"id": 5,
"parentId": 2,
"type": "C",
"childBCount": 0
},
{
"id": 6,
"parentId": 2,
"type": "C",
"childBCount": 0
}
]
我曾尝试使用graphlookup,但它没有为我提供向 graphlookup 阶段添加条件的选项。
我使用如下查询
modelName.aggregate([
{
$graphLookup: {
from: "modelName",
startWith: "$id",
connectFromField: "id",
connectToField: "parentId",
as: "children",
},
}
])
.allowDiskUse(true);
但是想要这样的东西,我可以在graphlookup阶段添加条件
modelName.aggregate([
{
$graphLookup: {
from: "modelName",
startWith: "$id",
connectFromField: "id",
connectToField: "parentId",
as: "children",
match:[
{
type:"B"
}
]
},
}
])
.allowDiskUse(true);
解决方案
$graphLookup 具有以下原型形式:
{
$graphLookup: {
from: <collection>,
startWith: <expression>,
connectFromField: <string>,
connectToField: <string>,
as: <string>,
maxDepth: <number>,
depthField: <string>,
restrictSearchWithMatch: <document>
}
}
这里<document>
遵循通常的查询过滤器语法,但需要注意的是您不能使用聚合表达式。
因此,您可以将条件添加到restrictSearchWithMatch
属性中,如下所示:
modelName.aggregate([
{
$graphLookup: {
from: "modelName",
startWith: "$id",
connectFromField: "id",
connectToField: "parentId",
as: "children",
restrictSearchWithMatch: { "type": "B" }
},
}
])
.allowDiskUse(true);
推荐阅读
- python - 仅当元素不为零时才快速合并两个数组数组的元素
- javascript - 颜色不在绘制的形状内
- r - 使用 R 使用 dbWriteTable 将具有 NA 值的数据推送到 Oracle 数据库的问题
- spring-boot - Spring Boot REST API 在客户端选择加载哪个字段的最佳方式
- reactjs - 根据屏幕大小动态调整 ImageList 列?
- r - R tibble 包:如何打印 tibble 的前 5 行和后 5 行(就像 data.table 一样)
- latex - Latex:方程的左对齐
- deep-learning - 有什么方法可以从 Pytorch 分布式(nccl)组中获得全球排名?
- c++ - 如何使用来自结构的成员模板化类型别名的模板模板参数
- android - 在多个网络调用场景中,对 Livedata/MediatorLiveData 进行链式操作的 android 有效方式是什么?