Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to do one-to-many "JOIN" in CouchDB
    primarykey
    data
    text
    <p>I am looking for a CouchDB equivalent to "SQL joins".</p> <p>In my example there are CouchDB documents that are list elements:</p> <pre><code>{ "type" : "el", "id" : "1", "content" : "first" } { "type" : "el", "id" : "2", "content" : "second" } { "type" : "el", "id" : "3", "content" : "third" } </code></pre> <p>There is one document that defines the list:</p> <pre><code>{ "type" : "list", "elements" : ["2","1"] , "id" : "abc123" } </code></pre> <p>As you can see the third element was deleted, it is no longer part of the list. So it must not be part of the result. Now I want a view that returns the content elements including the right order. </p> <p>The result could be:</p> <pre><code>{ "content" : ["second", "first"] } </code></pre> <p>In this case the order of the elements is already as it should be. Another possible result:</p> <pre><code>{ "content" : [{"content" : "first", "order" : 2},{"content" : "second", "order" : 1}] } </code></pre> <p>I started writing the map function:</p> <pre><code>map = function (doc) { if (doc.type === 'el') { emit(doc.id, {"content" : doc.content}); //emit the id and the content exit; } if (doc.type === 'list') { for ( var i=0, l=doc.elements.length; i&lt;l; ++i ){ emit(doc.elements[i], { "order" : i }); //emit the id and the order } } } </code></pre> <p>This is as far as I can get. Can you correct my mistakes and write a reduce function? Remember that the third document must not be part of the result.</p> <p>Of course you can write a different map function also. But the structure of the documents (one definig element document and an entry document for each entry) cannot be changed.</p> <hr> <p>EDIT: Do not miss JasonSmith's comment to his answer, where he describes how to do this shorter.</p>
    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.
 

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