MongoDB:如何用条件递归得到所有子节点的个数?

MongoDB: how to get the count of all the child nodes recursively with the condition?

如何根据条件递归得到所有子节点的个数?

我有这样一个集合的结构:

| 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);