Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Okay I finally understand what you are trying to do.Yes this is possible.Here is how.</p> <p>You have 2 documents</p> <pre><code>{ "_id":"anyvalue", "type": "track", "title": "Bohemian Rhapsody" } { "_id":"2", "type": "artist", "name": "Queen", "tracks": ["anyvalue"] } </code></pre> <p>What you were doing wrong was not having quotes around the value of tracks(the item in the array).</p> <p>2)The reference id must be _id for this to work.The difference is worth noting since you can have id field but only _id are used to identify documents.</p> <p>For the result you want this view would suffice</p> <pre><code>function(doc) { if (doc.type === 'artist') { for (var i in doc.tracks) { var id = doc.tracks[i]; emit(id, { _id: id }); } } } </code></pre> <p>What you want to be doing is use an emit function inside the for loop to emit the id field of the 'track' of every artist.</p> <p>Then you want to query couch db view with the include_docs=true parameter.Here is the final result for the database that you created on iris couch.</p> <p><a href="http://jphastings.iriscouch.com/music/_design/test/_view/nested?reduce=false&amp;include_docs=true" rel="nofollow noreferrer">http://jphastings.iriscouch.com/music/_design/test/_view/nested?reduce=false&amp;include_docs=true</a></p> <pre><code> { "total_rows": 3, "offset": 0, "rows": [ { "id": "0b86008d8490abf0b7e4f15f0c6a50a7", "key": "0b86008d8490abf0b7e4f15f0c6a463b", "value": { "_id": "0b86008d8490abf0b7e4f15f0c6a463b" }, "doc": { "_id": "0b86008d8490abf0b7e4f15f0c6a463b", "_rev": "3-7e4ba3bfedd29a07898125c09dd7262e", "type": "track", "title": "Boheniam Rhapsody" } }, { "id": "0b86008d8490abf0b7e4f15f0c6a50a7", "key": "0b86008d8490abf0b7e4f15f0c6a5ae2", "value": { "_id": "0b86008d8490abf0b7e4f15f0c6a5ae2" }, "doc": { "_id": "0b86008d8490abf0b7e4f15f0c6a5ae2", "_rev": "2-b3989dd37ef4d8ed58516835900b549e", "type": "track", "title": "Another one bites the dust" } }, { "id": "0b86008d8490abf0b7e4f15f0c6a695e", "key": "0b86008d8490abf0b7e4f15f0c6a6353", "value": { "_id": "0b86008d8490abf0b7e4f15f0c6a6353" }, "doc": { "_id": "0b86008d8490abf0b7e4f15f0c6a6353", "_rev": "2-0383f18c198b813943615d2bf59c212a", "type": "track", "title": "Stripper Vicar" } } ] } </code></pre> <p>Jason explains it wonderfully in this post</p> <p><a href="https://stackoverflow.com/questions/3033443/best-way-to-do-one-to-many-join-in-couchdb">Best way to do one-to-many &quot;JOIN&quot; in CouchDB</a></p> <p>this link is also helpful for entity relationships in couch db</p> <p><a href="http://wiki.apache.org/couchdb/EntityRelationship" rel="nofollow noreferrer">http://wiki.apache.org/couchdb/EntityRelationship</a></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