title: RavenDB and hierarchical documents
body: <p>I am having difficulties solving this problem using RavenDB. I have these classes.
  I excluded a lot of properties from them to keep the example simple.</p>
  
  <pre><code>public class Menu
  {
      public string Name { get; set; }
      public List&lt;NavigationNode&gt; Nodes { get; set; }
  }
  
  public class NavigationNode
  {
      public string Text { get; set; }
      public Guid? PageId { get; set; }
      public string NodeType { get; set; }
      public List&lt;NavigationNode&gt; Nodes { get; set; }
  }
  
  public class Page
  {
      public Guid PageId { get; set; }
      public string Slug { get; set; }
  }
  </code></pre>
  
  <p>So as you can see this is about rendering a navigation menu. Node lists are hierarchical and can go deep, in theory infinitely(of course in reality 2-4 sub levels only). At first i stored the Slug in the Node but realised that, what happens if the page Slug changes, changing all nodes when a page changes slug i would have to loop through all menus, climb down the hierarchy and find all those Slug values to change them, which does not sound like an optimal solution.</p>
  
  <p>So i was thinking it should be possible to build an index that combines the Page Slug with rest of the data from the Node in a hierarchical structure.</p>
  
  <p>I have been reading about Map Reduce, Multimap and recurse but i am not even sure where to start.</p>
  
  <p>I found this <a href="http://ravendb.net/docs/2.0/client-api/querying/static-indexes/indexing-hierarchies" rel="nofollow">http://ravendb.net/docs/2.0/client-api/querying/static-indexes/indexing-hierarchies</a></p>
  
  <p>Here is a simple example i tried just to get something started, and i cannot even get that to work since i really don't understand the example on the page i linked to above.</p>
  
  <pre><code>  public class NavigationIndex : AbstractIndexCreationTask&lt;Menu&gt;
     {
          public NavigationIndex()
          {
              Map = menus =&gt; from menu in menus
                             from node in Recurse(menu, x =&gt; x.Nodes)
                             select new
                                        {
                                            WhatIsThis = node // &lt;- Why is this a collection?
                                        };
          }
      }
  </code></pre>
  
  <p>According to the example node should not be a collection but an actual NavigationNode object.</p>
  
  <p>Is it possible to achieve what i want in RavenDB and what am i doing wrong in the example?</p>
  
  <p>Please feel free to ask anything you find confusing.</p>
  
  <p>I apologize for the confusion. I'll try to explain it.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Changing PageId to string will not be a problem. I am using Guids since i need to be able to generate primary key ID:s before inserting. Anyway, what i want to Query for from the index is a hiearachical tree of navigation links where the Pages Slug is included. So i can recursively loop out a navigation menu on the website.</p>
  
  <p>Answers to Matt Johnson's questions:</p>
  
  <ol>
  <li>I want a output of a class you can see below</li>
  <li>Page is a separate document</li>
  <li><p>I am only going to query by Menu Name</p>
  
  <p>public class NavigationIndexItem{
      public string MenuName { get; set; }
      public string Text { get; set; }
      public string Slug { get; set; }
      public string NodeType { get; set; }
      public List ChildItems { get; set; }
      }</p></li>
  </ol>
  
  <p>Now when i see the above class i think i might a bit on the wrong path. </p>
  
  <p>But anyway i will make some minor changes and thank you Matt for answering. However i am still running into same problem as before.</p>
  
  <p><strong>This row in your example:  where node.PageId != null</strong>    </p>
  
  <p>node is not an instance of a specific NavigationNode but yet another collection so i cannot check the PageId property on it. I only get a list of LINQ extensions.</p>