I have the following structure for my documents:
[{
"_id": "1",
"prop1": "..."
}, {
"_id": "2",
"prop1": "...",
"derivationOf": "1"
}, {
"_id": "3",
"prop1": "...",
"derivationOf": "1"
}, {
"_id": "4",
"prop1": "...",
"derivationOf": "3"
}, {
"_id": "5",
"prop1": "...",
"derivationOf": "4"
}]
I would like to perform a recurisve query that would produce a tree-like version with the same data, like so:
{
"_id": "1",
"prop1": "...",
"children": [{
"_id": "2",
"prop1": "...",
"children": []
}, {
"_id": "3",
"prop1": "...",
"children": [{
"_id": "4",
"prop1": "...",
"children": [{
"_id": "5",
"prop1": "...",
"children": []
}]
}]
}]
}
I have looked at $graphLookup
and this question but without much success.
Does anyone know how to achieve this with a mongodb query?