You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Devon Weller <dw...@devonweller.com> on 2009/11/05 20:51:55 UTC
Aggregate Sum Example is Broken
Hi.
I believe the aggregate sum example in the couchdb wiki does not work
in CouchDB 0.10.
I am trying to implement the view snippet as shown on this page:
http://wiki.apache.org/couchdb/View_Snippets#aggregate_sum
Here is my database:
[
{"_id":"1","Type":"customer","name":"user1","address":"123 Somewhere
St.","zip":"12345"},
{"_id":"2","Type":"customer","name":"user2","address":"456 Somewhere
St.","zip":"23456"},
{"_id":"3","Type":"order","customer_id":"1"},
{"_id":"4","Type":"order","customer_id":"1"}
]
(ids shortened for clearer understanding)
Here is the map function:
function(doc) {
if (doc.Type == "customer") {
emit([doc._id, 0], doc);
} else if (doc.Type == "order") {
emit([doc.customer_id, 1], doc);
}
}
Here is the reduce function:
// Reduce function
// Only produces meaningful output.customer_details if group_level >= 1
function(keys, values, rereduce) {
var output = {};
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 =
values[idx];
else if (values[idx].Type == "order") output.total += 1;
}
}
return output;
}
// note that I made one correction at "output.customer_details = values
[idx];"
When I call:
http://localhost:5984/mydb/_design/mydb/_view/aggregate_sum?group_level=1
I get this error:
{"error":"reduce_overflow_error","reason":"Reduce output must shrink
more rapidly: Current output: '[{\"customer_details\": {\"_id\":
\"1656635ff2687c1d5ef0dd799e74aaf9\",\"_rev\":
\"1-652a40bc314cda302d9faf1b'... (first 100 of 207 bytes)"}
My question is this:
Should this example work and couchdb is broken? Or is the example bad?
I am running CouchDBX-0.10.0-R13B02-64bit-Snow-Leopard.
Thanks,
- Devon
Re: Aggregate Sum Example is Broken
Posted by Mirsal Ennaime <mi...@gmail.com>.
On Thu, Nov 5, 2009 at 8:51 PM, Devon Weller <dw...@devonweller.com> wrot
>
> Hi.
Hello
> My question is this:
>
> Should this example work and couchdb is broken? Or is the example bad?
The example is bad imho. Do not output the document in your reduce
function. If you need it, use include_docs=true in your query
--
Mirsal Ennaime