couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Couchdb Wiki] Update of "View Snippets" by JasonDavies
Date Sun, 04 Jan 2009 19:05:13 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The following page has been changed by JasonDavies:
http://wiki.apache.org/couchdb/View_Snippets

The comment on the change is:
Added map/reduce join example from IRC

------------------------------------------------------------------------------
  
  When querying this reduce you should not use the `group` or `group_level` query string parameters.
The returned reduce value will be an object with the top `MAX` tag: count pairs.
  
+ == Joining an aggregate sum along with related data ==
+ 
+ Here is a modified example from [wiki:View_collation].  Note that `group_level` needs to
be set to `1` for it to return a meaningful `customer_detailsĀ§.
+ 
+ {{{
+ // Map function
+ function(doc) {
+   if (doc.Type == "customer") {
+     emit([doc._id, 0], doc);
+   } else if (doc.Type == "order") {
+     emit([doc.customer_id, 1], doc);
+   }
+ }
+ 
+ // Reduce function
+ // Only produces meaningful output.customer_details if group_level >= 1
+ function(keys, values, rereduce) {
+   var output = {total: 0, customer_details: null};
+   if (rereduce) {
+     for (idx in values) {
+       if (values[idx].total !== undefined) {
+         output.total += values[idx].total;
+       } else if (values[idx].customer_details !== undefined) {
+         output.customer_details = values[idx].customer_details;
+       }
+     }
+   } else {
+     for (idx in values) {
+       if (values[idx].Type == "customer") output.customer_details = doc;
+       else if (values[idx].Type == "order") output.total += 1;
+     }
+   }
+   return output;
+ }
+ }}}
+ 

Mime
View raw message