title: MongoDB - children and parent structure
body: <p>Having just recently delved into the world of NoSQL with MongoDB, I am still struggling to understand the best approach to architecture without 3rd normalizing the data and then joining upon it.  Currently the project I am designing is a simple collection of articles, akin to a wiki.  An article will have a title and text, as well as (possibly) a parent article and one or more children articles.  </p>
  
  <p>I have had a number of differing ideas of for the database design and want to pick the one that best matches the strenghts of MongoDB.</p>
  
  <p><strong>Idea One</strong></p>
  
  <p>Since the most frequent type of query on the database will invariably be to simply retrieve an article, I am embedding all the relevant data that the page will need in order to display everything.  The actual article of course, as well as a parent subdocument with a <strong>url</strong> (which will match the _id of some other document) as well as a <strong>title</strong> which is the text that we will print out on screen for inside of the  tag.  An identicle structure exists for the <strong>children</strong> except that it is an array so that all of the <strong>children</strong> are there.</p>
  
  <pre><code>{
          "_id" : "test-article-2",
          "title" : "Test Article 2",
          "text" : "Blah 2",
          "parent" : {
                  "title" : "Test Article",
                  "url" : "test-article"
          },
          "children" : [
                  {
                          "title" : "Test Article 3",
                          "url" : "test-article-3"
                  }
          ]
  }
  </code></pre>
  
  <p>This type of design seems to have the advantage of speed (in my opinion) but I would like to hear what others thing of this design.</p>
  
  <p><strong>Idea Two</strong></p>
  
  <p>More along the lines that I am used to coming from a relational database world. would be to not embed sub-objects into the design but to simply put in their unique identifiers.  Thus <strong>parent</strong> now contains just a text string which will match the <strong>_id</strong> of some other document, and <strong>children</strong> will similarly have an array of strings which link to <strong>_id</strong>s.  </p>
  
  <p>In order to get all of the information to view an article however, we would now need to make a number of queries (at least I think we need to...) One to get the main article, then another to get the parent's title for putting in the  tag and then another to get all of the children articles and likewise get their titles.</p>
  
  <p>This seems like a lot more querying just to display an article, but it may make the updating of the database easier, if for instance some article is deleted or updated. (again not sure on that point).</p>
  
  <pre><code>{
          "_id" : "test-article-2",
          "title" : "Test Article 2",
          "text" : "Blah 2",
          "parent" : "test-article",
          "children" : [ "test-article-3", "test-article-4"]
  }
  </code></pre>
  
  <p>Would be glad to hear the input of those with more experience with MongoDB designing.  </p>