Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoDB Storing deeply nested tree - How to query nested document?
    text
    copied!<p>I am trying to build a schema to store a website navigation map. Picture the typical dataset as a Sitemap where pages can have child pages, and those child pages can have more children. An example would be the following object:</p> <pre><code>$tree = array( 'home' =&gt; array( 'h1' =&gt; 'test', 'h2' =&gt; 'test2', 'copy' =&gt; 'copy here', 'slug' =&gt; '', 'children' =&gt; array( 'blah' =&gt; array( 'h1' =&gt; 'child1', 'h2' =&gt; 'child1', 'copy' =&gt; 'child copy here', 'slug' =&gt; 'blah', 'children' =&gt; array( 'blahsub' =&gt; array( 'h1' =&gt; 'subchild1', 'h2' =&gt; 'subchild2', 'copy' =&gt; 'child copy here', 'slug' =&gt; 'blahsub', 'children' =&gt; array( 'subsubchild1' =&gt; array( 'h1' =&gt; 'subsubchild1', 'h2' =&gt; 'subsubchild2', 'copy' =&gt; 'child copy here', 'slug' =&gt; 'subsubchild1' ) ) ), 'subchild2' =&gt; array( 'h1' =&gt; 'subchild1', 'h2' =&gt; 'subchild2', 'copy' =&gt; 'child copy here', 'slug' =&gt; 'subchild2', 'children' =&gt; array( 'subsubchild2' =&gt; array( 'h1' =&gt; 'subsubchild1', 'h2' =&gt; 'subsubchild2', 'copy' =&gt; 'child copy here', 'slug' =&gt; 'subsubchild2' ) ) ), 'subchild3' =&gt; array( 'h1' =&gt; 'subchild1', 'h2' =&gt; 'subchild2', 'copy' =&gt; 'child copy here', 'slug' =&gt; 'subchild3', 'children' =&gt; array( 'subsubchild3' =&gt; array( 'h1' =&gt; 'subsubchild1', 'h2' =&gt; 'subsubchild2', 'copy' =&gt; 'child copy here', 'slug' =&gt; 'subsubchild3' ) ) ) ) ), 'another' =&gt; array( 'h1' =&gt; 'child2', 'h2' =&gt; 'child2', 'copy' =&gt; 'child copy here', 'slug' =&gt; 'another' ) ) ) ); </code></pre> <p>Each level of the $tree is indexed by it's URI, and each level can contain N children, all of which can have children themselves.</p> <p>My original thought was that using this schema is intuitive, however I am unsure about querying for specific nodes of the tree. Am I approaching this schema wrong? (The goal was not to have to make multiple queries to "build" such a tree on each request, instead to pull it once and work with it in-memory. </p> <p>Note: I still can make a query to pull the entire tree, and then access each node directly via the subscript operator. This should give me better performance than multiple queries into the tree. Currently using mysql for this, and its a real bottleneck.</p> <p>Please share your opinions on this schema, and ask for clarification if something was unclear.</p> <p>Thanks!</p> <p>Note - Main Objective: I would like to find items in the tree by their "index" (also the "slug" parameter of each node). I really am at a lose of where to start - does such a querying functionality exist? Must it be done in a loop with multiple queries?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload