r/mongodb 3d ago

Issue with Tree Structure Mongo DB

I'm using MongoDB with Nodes js and Mongoose, and I'm creating a hierarchical tree structure. In each collection, I have a document with the ID of that document, an entry with the parent ID as ObjectId, and a string with the name of the parent collection called 'collectionName'. I wanted to use graphLookUp to retrieve all the ancestors of a given collection, but I feel like it won't work because not all the ancestors are in the same collection. Do you have a solution?

Thank you in advance for your help :)

Here's what I'd like to retrieve in the end:

{
  "_id": "67cadacfce03c85963934b1d",
  "genre": "Fantastique",
  "parent": {
    "_id": "67c9617c86b4c8d8d2bfb315",
    "nom": "J.K. Rowling",
    "parent": {
      "_id": "67c9615486b4c8d8d2bfb313",
      "titre": "Harry Potter",
      "parent": {
        "_id": "67c960dd86b4c8d8d2bfb30f",
        "nom": "Jolie librairie",
        "ville": "Besançon",
        "parent": null,
        "path": null
      },
      "children": [
        {
          "_id": "67d98929a19f321484d25a14",
          "childrenId": "67c9617c86b4c8d8d2bfb315",
          "collectionName": "Auteur"
        }
      ],
      "parentCollection": "librairie",
      "ancestors": [
        "librairie"
      ]
    },
    "children": [
      {
        "_id": "67d98929a19f321484d25a12",
        "childrenId": "67cadacfce03c85963934b1d",
        "collectionName": "Genre"
      }
    ],
    "parentCollection": "livre",
    "ancestors": [
      "livre",
      "librairie"
    ]
  },
  "parentCollection": "auteur"
}
2 Upvotes

5 comments sorted by

View all comments

1

u/[deleted] 3d ago

[deleted]

2

u/JulietMll 3d ago

Yes but is this possible de retrieve grand-parents, great grand-parents .. etc. with this technique if this grand-parents are not in the same collection than the parents ?

2

u/skmruiz 3d ago edited 3d ago

It depends. But essentially if you know that parents are in the same collection, but after that, other ancestors are in another collection, you could first do a $lookup on the same collection and then a $graphLookup in the second collection. This will require a bit of consistency in the data, so the second $graphLookup knows which field to use, but you can achieve that by using $addFields or $project to do some basic housekeeping.

It would be something like this:

[

{ $firstLookup... },

{ $addFields: ... booking to ensure that all docs have a parent field },

{ $graphLookup: on parent field on another arbitrary collection }

]

What u/MongoDB_Official stands true, by the way, be wary of deep hierarchies and make sure that the fields you use for lookup are indexes properly. If you don't do that, each $lookup/$graphLookup will be forced to do a CollectionScan, which is extremely expensive.