Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update</strong></p> <blockquote> <p>Note on JavaScript API change: Prior to Tue, 20 May 2008 (Subversion revision r658405) the function to emit a row to the map index, was named "map". It has now been changed to "emit".</p> </blockquote> <p>That's the reason why there is <code>map</code>used instead of <code>emit</code>it was renamed. Sorry I corrected my code to be valid in the recent version of CouchDB.</p> <p><strong>Edit</strong></p> <p>I think what you are looking for is a has-many relationship or a join in sql db language. Here is a <a href="http://www.cmlenz.net/archives/2007/10/couchdb-joins" rel="nofollow">blog</a> article by Christopher Lenz that describes exactly what your options are for this kind of scenario in CouchDB.</p> <p>In the last part there is a technique described that you can use for the list you want. You need a map function of the following format</p> <pre><code>function(doc) { if (doc.type == "text_file") { emit([doc._id, 0], doc); } else if (doc.type == "text_fragment") { emit([doc.file_id, 1], doc); } } </code></pre> <p>Now you can query the view in the following way: </p> <pre><code>my_view?startkey=["text_file_id"]&amp;endkey;=["text_file_id", 2] </code></pre> <p>This gives you a list of the form</p> <ul> <li>text_file</li> <li>text_fragement_1</li> <li>text_fragement_2</li> <li>..</li> </ul> <p><strong>Old Answer</strong></p> <p>Directly from the <a href="http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views#Reduce_Functions" rel="nofollow">CouchDB Wiki</a></p> <pre><code>function (key, values, rereduce) { return sum(values); } </code></pre> <p>Reduce functions are passed three arguments in the order key, values and rereduce</p> <p>Reduce functions must handle two cases:</p> <ol> <li><p><strong>When rereduce is false:</strong></p> <ul> <li>key will be an array whose elements are arrays of the form [key,id], where key is a key emitted by the map function and id is that of the document from which the key was generated.</li> <li>values will be an array of the values emitted for the respective elements in keys i.e. <code>reduce([ [key1,id1], [key2,id2], [key3,id3] ], [value1,value2,value3], false)</code></li> </ul></li> <li><p><strong>When rereduce is true:</strong></p> <ul> <li>key will be null</li> <li>values will be an array of values returned by previous calls to the reduce function i.e. <code>reduce(null, [intermediate1,intermediate2,intermediate3], true)</code> Reduce functions should return a single value, suitable for both the value field of the final view and as a member of the values array passed to the reduce function.</li> </ul></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