如何创建以下指定输出?

How can I create the below specify output?

你好下面我指定了要使用的数据并得到了我在下面提到的输出,你能帮我解决这个问题吗,因为我尝试了很多但找不到方法。

//This is the data
let data = [
  {'name': 'A', 'parent': null},
  {'name': 'B', 'parent': null},
  {'name': 'C', 'parent': 'A'},
  {'name': 'D', 'parent': 'A'},
  {'name': 'E', 'parent': 'D'},
  {'name': 'F', 'parent': 'D'},
  {'name': 'G', 'parent': 'B'},
  {'name': 'H', 'parent': 'B'},
 ];

//And want the output like this by using mongodb aggregation
{
 "null": [
  {
   "A": [
    {
     "C": []
    },
    {
     "D": [
      {
       "E": []
      },
      {
       "F": []
      }
     ]
    }
   ]
  },
  {
   "B": [
    {
     "G": []
    },
    {
     "H": []
    }
   ]
  }
 ]
}

我尝试过使用 graphlookup 聚合,但找不到解决方法。

感谢您的帮助!

你可以这样做。

  db.collection.aggregate([
  {
    $graphLookup: {
      from: "collection",
      startWith: "$name",
      connectFromField: "name",
      connectToField: "parent",
      as: "graph"
    }
  },
  {
    "$project": {
      name: 1,
      parent: 1
    }
  },
])

如果您想以数组形式显示它,您可以创建对象并使用 $objectToArray 进行引用检查 link。 $objectToArray

Solution in Mongoplayground.

试试这个:

db.collection.aggregate([
    {
        $lookup: {
            from: "collection",
            let: { name: "$name" },
            pipeline: [
                {
                    $match: {
                        $expr: { $eq: ["$parent", "$$name"] }
                    }
                },
                {
                    $lookup: {
                        from: "collection",
                        let: { name: "$name" },
                        pipeline: [
                            {
                                $match: {
                                    $expr: { $eq: ["$parent", "$$name"] }
                                }
                            },
                            {
                                $replaceRoot: {
                                    newRoot: {
                                        $arrayToObject: [[{ k: "$name", v: [] }]]
                                    }
                                }
                            }
                        ],
                        as: "children"
                    }
                },
                {
                    $replaceRoot: {
                        newRoot: {
                            $arrayToObject: [[{ k: "$name", v: "$children" }]]
                        }
                    }
                }
            ],
            as: "children"
        }
    },
    {
        $match: {
            children: { $ne: [] },
            parent: null
        }
    },
    {
        $group: {
            _id: "$parent",
            array: {
                $push: {
                    array: [{ k: "$name", v: "$children" }]
                }
            }
        }
    },
    {
        $addFields: {
            array: {
                $map: {
                    input: "$array",
                    as: "item",
                    in: { $arrayToObject: "$$item.array" }
                }
            }
        }
    },
    {
        $replaceRoot: {
            newRoot: { $arrayToObject: [[{ k: "null", v: "$array" }]] }
        }
    }
]);

输出:

{
    "null" : [
        {
            "A" : [
                {
                    "C" : [ ]
                },
                {
                    "D" : [
                        {
                            "E" : [ ]
                        },
                        {
                            "F" : [ ]
                        }
                    ]
                }
            ]
        },
        {
            "B" : [
                {
                    "G" : [ ]
                },
                {
                    "H" : [ ]
                }
            ]
        }
    ]
}