1

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?

M.M
  • 2,254
  • 1
  • 20
  • 33
  • 1
    You can take a look at this answer: https://stackoverflow.com/a/52662426/6238977 Basically it's the same problem – mickl Jan 06 '19 at 19:12

0 Answers0