As presented in https://www.slideshare.net/mongodb/webinar-working-with-graph-data-in-mongodb, slide 50 it is possible to use $graphLookup on a
Unfortunately, you can't get the full depth in a nested format. Using a view is a workaround which lets you perform that operation, but you would need to create a new view for each level of embedding that you need. Instead, I would consider performing a graphLookup without providing a depth, starting from the root level, fetching all the hierarchy in a single query, before computing the tree at the application level.
This would look like something like this:
db.node.aggregate([
{ $match: {
parentId: null
}},
{ $graphLookup: {
from: "node",
startWith: "$nodeId",
connectFromField: "nodeId",
connectToField: "parentId",
depthField: "depth",
as: "children"
}}
]);
This should let you fetch the whole hierarchy in one go, so next, you need to calculate the tree in your application, from the information you will have in the "children" array.