Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thank you! This is a great example to show off <a href="https://stackoverflow.com/questions/2683146/what-are-the-major-new-features-of-couchdb-0-11">CouchDB 0.11's new features</a>!</p> <p>You must <strong>use the fetch-related-data feature to reference documents in the view.</strong> Optionally, for more convenient JSON, use a <code>_list</code> function to clean up the results. See <a href="http://blog.couchbase.com/whats-new-in-apache-couchdb-0-11-part-two-views" rel="nofollow noreferrer">Couchio's writeup on "JOIN"s</a> for details.</p> <p>Here is the plan:</p> <ol> <li><p>Firstly, you have a uniqueness contstraint on your <code>el</code> documents. If two of them have id=2, that's a problem. It is necessary to use the <code>_id</code> field instead if <code>id</code>. CouchDB will guarantee uniqueness, but also, the rest of this plan requires <code>_id</code> in order to fetch documents by ID.</p> <pre><code>{ "type" : "el", "_id" : "1", "content" : "first" } { "type" : "el", "_id" : "2", "content" : "second" } { "type" : "el", "_id" : "3", "content" : "third" } </code></pre> <p>If changing the documents to use <code>_id</code> is absolutely impossible, you can create a simple view to <code>emit(doc.id, doc)</code> and then re-insert that into a temporary database. This converts <code>id</code> to <code>_id</code> but adds some complexity.</p></li> <li><p>The view emits <code>{"_id": content_id}</code> data keyed on <code>[list_id, sort_number]</code>, to "clump" the lists with their content.</p> <pre><code>function(doc) { if(doc.type == 'list') { for (var i in doc.elements) { // Link to the el document's id. var id = doc.elements[i]; emit([doc.id, i], {'_id': id}); } } } </code></pre> <p>Now there is a simple list of <code>el</code> documents, in the correct order. You can use <code>startkey</code> and <code>endkey</code> if you want to see only a particular list.</p> <pre><code>curl localhost:5984/x/_design/myapp/_view/els {"total_rows":2,"offset":0,"rows":[ {"id":"036f3614aeee05344cdfb66fa1002db6","key":["abc123","0"],"value":{"_id":"2"}}, {"id":"036f3614aeee05344cdfb66fa1002db6","key":["abc123","1"],"value":{"_id":"1"}} ]} </code></pre></li> <li><p>To get the <code>el</code> content, query with <code>include_docs=true</code>. Through the magic of <code>_id</code>, the <code>el</code> documents will load.</p> <pre><code>curl localhost:5984/x/_design/myapp/_view/els?include_docs=true {"total_rows":2,"offset":0,"rows":[ {"id":"036f3614aeee05344cdfb66fa1002db6","key":["abc123","0"],"value":{"_id":"2"},"doc":{"_id":"2","_rev":"1-4530dc6946d78f1e97f56568de5a85d9","type":"el","content":"second"}}, {"id":"036f3614aeee05344cdfb66fa1002db6","key":["abc123","1"],"value":{"_id":"1"},"doc":{"_id":"1","_rev":"1-852badd683f22ad4705ed9fcdea5b814","type":"el","content":"first"}} ]} </code></pre> <p>Notice, this is already all the information you need. If your client is flexible, you can parse the information out of this JSON. The next <em>optional</em> step simply reformats it to match what you need.</p></li> <li><p>Use a <code>_list</code> function, which simply reformats the view output. People use them to output XML or HTML however we will make the JSON more convenient.</p> <pre><code>function(head, req) { var headers = {'Content-Type': 'application/json'}; var result; if(req.query.include_docs != 'true') { start({'code': 400, headers: headers}); result = {'error': 'I require include_docs=true'}; } else { start({'headers': headers}); result = {'content': []}; while(row = getRow()) { result.content.push(row.doc.content); } } send(JSON.stringify(result)); } </code></pre> <p>The results match. Of course in production you will need <code>startkey</code> and <code>endkey</code> to specify the list you want.</p> <pre><code>curl -g 'localhost:5984/x/_design/myapp/_list/pretty/els?include_docs=true&amp;startkey=["abc123",""]&amp;endkey=["abc123",{}]' {"content":["second","first"]} </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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