'Searching tree of nested objects

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?



Solution 1:[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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gibbs