1

I'm trying to understand how to search within a tree of objects. I've looked at various search tutorials but I can't figure it out.

Basically, I've got a structure in which a node can have any number of child nodes, which in turn can have any number of child nodes. (I do understand that they can only be nested up to 100 deep.)

Consider a document with this structure:

  {
    "title": "food",
    "children": [
      {
        "title": "fruit",
        "children": [
          {
            "title": "red",
            "children": [
              { "title": "cherry", "children":[] },
              { "title": "apple", "children":[] },
              { "title": "raspberry", "children":[] },
              { "title": "pomegranate", "children":[] }
            ]
          },
          {
            "title": "yellow",
            "children": [
              { "title": "banana", "children":[] }
            ]
          }
        ]
      },
      {
      "title": "spices",
        "children": [
          { "title": "paprika", "children":[] },
          {
            "title": "pepper",
            "children": [
              { "title": "java", "children":[] },
              { "title": "matico", "children":[] }
            ]
          }
        ]
      }
    ]
  }

Now, suppose I want to find that node that has the title "matico". I just want to return that node, not the entire structure. How is that done?

tscheingeld
  • 789
  • 7
  • 24
  • i guess when the tree has arbitrary depth, it's not possible by available query logic of mongodb apis, maybe load all docs out and do recursion – Dee May 20 '22 at 09:38
  • Does this answer your question? [MongoDB - get subdocuments from arbitrary depth and object structure](https://stackoverflow.com/questions/72173488/mongodb-get-subdocuments-from-arbitrary-depth-and-object-structure) – nimrod serok May 20 '22 at 09:44
  • Yes, it does! It shouldn't be too hard to set up a recursive search. The structures shouldn't be too huge for that to create a significant delay. – tscheingeld May 20 '22 at 18:19

1 Answers1

1

As per the documentation

MongoDB supports no more than 100 levels of nesting for BSON documents. Each object or array adds a level.

Mongodb is not fit for this schema. Hence, you cannot achieve this with huge dynamic levels of deeply nested arrays. You can alter the schema to have flattened structure or data store.

Gibbs
  • 21,904
  • 13
  • 74
  • 138