I have mongo tree structure that looks like this:
{\"parent\": null, \"path\": \"#a\", \"name\": \"a\"}
{\"parent\": \"a\", \"path\": \"#a#b\", \"name\": \"b\"}
connectToField
is a name, not an expression. You can't do anything with it.
You really need to re-consider the schema. It is flawed in many ways starting from non-unique names used in parent references. Since you rely on the path string instead (which is a questionable decision by itself), you need a path to reference the parent.
The answer below does it runtime, and is hardly advisable for operational queries due to performance inefficiency and some assumptions of how the path is built. It can be used as a one-off request though.
Basically you need to create a view with calculated parent path:
db.createView("rootless_tree", "tree", [
{ $match: { parent: { $ne: null } } },
{ $addFields: {
parent_path: { $let: {
vars: { parents: { $split: [ "$path", "#" ] } },
in: { $reduce: {
input: { $slice: [ "$$parents", 1, { $subtract: [ { $size: "$$parents" }, 2 ] } ] },
initialValue: "",
in: { $concat: [ "$$value", "#", "$$this" ] }
} }
} }
} }
]);
So then you can do your lookup as advised in your previous question:
db.tree.aggregate([
{ $graphLookup: {
from: "rootless_tree",
startWith: "$path",
connectFromField: "path",
connectToField: "parent_path",
as:"dep"
} },
{ $match: { dep: [] } },
])