You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by Apache Wiki <wi...@apache.org> on 2009/01/04 20:05:13 UTC
[Couchdb Wiki] Update of "View Snippets" by JasonDavies
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;
+ }
+ }}}
+