title: How to structure for multi-level nesting in mongodb using mongoose
body: <p>Being and sql database user for many years I am having trouble working out how best to structure my data in mongodb using mongoose. As a practice project I am putting together a basic cms and I am stuck on how best to structure my page schema in order to allow nesting to multiple levels while maintaining the ability to access both parents and children and also edit any page at any level.</p>
  
  <p>My structure would be something like this:</p>
  
  <pre><code>page 1 --- sub page 1
    |    |
    |     -- sub page 2
    |
    |
  page 2 --- sub page 1 --- sub-sub page 1
    |    |               |
    |     -- sub page 2   - sub-sub page 2 --- sub-sub-sub page 1
    |    |                                  |
    |     -- sub page 3                      - sub-sub-sub page 2
    |    |
    |     -- sub page 4
    |
    |
  page 3 --- sub page 1
          |
           - sub page 2
          |
           - sub page 3
  </code></pre>
  
  <p>I have put together a basic single level schema as a starting point and I am able to query and update this easily:</p>
  
  <pre><code>var pageSchema = new Schema({
      name: {
         type: String,
         required: true
      },
      url: {
          type: String,
          required: true
      },
      order: {
          type: Number,
          default: 10000
      }
  });
  </code></pre>
  
  <p>The problem comes when figuring out how to add sub pages. Coming form sql my instinct would be to add a parent_id field, but I am aware this is a very flat way of thinking about the issue and probably not in the spirit of nosql.</p>
  
  <p>Reading around my first attempt was to use populate (<a href="http://mongoosejs.com/docs/populate.html" rel="nofollow noreferrer">http://mongoosejs.com/docs/populate.html</a>. For this I tried adding an extra field to my scheme.</p>
  
  <pre><code>children : [{ type: Schema.Types.ObjectId, ref: 'children' }] 
  </code></pre>
  
  <p>This seemed like the right approach at first, until I started thinking about the issue of recursive population. Firstly, to use populate with only one schema means a lot of repetition and also, for my structure to work I have to be able to allow for setting and retrieving content nested to any number of levels. This means I would need some sort of recursive populate. This sounded really painful and indeed when I tried searching for solutions I came across this question which makes it clear that doing this is going to result in horrible performance.</p>
  
  <p><a href="https://stackoverflow.com/questions/26041262/mongoose-recursive-populate">mongoose recursive populate</a></p>
  
  <p>This second question talks about using tree documents, but I am struggling to understand the approach.  I tried searching for further questions related to this subject, but usually the answers just point to a pre build npm packaged solution. These are probably good for getting up and running quickly but my project is purely for learning about structuring in nosql so I am not just looking for the best solution, but to also understand it.</p>
  
  <p>Can someone suggest how to best structure the data in my case, and/or point me in the direction of some documentation that will put me on the right track.</p>