title: CouchDB's Linked Documents in a View
body: <p>I'm having a hard time getting my head around CouchDB's <a href="http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views#Linked_documents" rel="nofollow">linked documents</a> feature.</p>
  
  <p>I have two <code>types</code> of data being stored in a single CouchDB database:</p>
  
  <pre><code>{
    "id":"1",
    "type": "track",
    "title": "Bohemian Rhapsody"
  }
  
  {
    "id":"2",
    "type": "artist",
    "name": "Queen",
    "tracks": ["1"]
  }
  </code></pre>
  
  <p>I'm under the impression that I can write a view like the one below and get the following documents emited:</p>
  
  <pre><code>{
    "id":"2",
    "type": "artist",
    "name": "Queen",
    "tracks": [
      {
        "id":"1",
        "type": "track",
        "title": "Bohemian Rhapsody"
      }
    ]
  }
  </code></pre>
  
  <p>I've been trying this view, but it's not working the way I'm expecting:</p>
  
  <pre><code>function(doc) {
    if(doc.type == 'artist') {
      var tracks = [];
      for(var i = 0; i &lt; doc.tracks.length; i++) {
        tracks.push({_id:doc.tracks[i]});
      }
  
      newdoc = eval(uneval(doc));
      newdoc.tracks = tracks;
  
      emit(doc._id,newdoc);
    }
  }
  </code></pre>
  
  <p>example here: <a href="http://jphastings.iriscouch.com/_utils/database.html?music/_design/test/_view/linked" rel="nofollow">http://jphastings.iriscouch.com/_utils/database.html?music/_design/test/_view/linked</a></p>
  
  <p>This isn't returning what I'd hope - do you have any suggestions? Thanks</p>