incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kinley Dorji <>
Subject Re: Reduce function to perform a Union?
Date Fri, 20 May 2011 09:32:46 GMT
In your example, you want to string together the different objects
corresponding to key abc123. I'm not sure how you worked your reduce
code but would suggest that if you probably need code like this in it

function(key, values, rereduce) {
  var obj = {};
  // iterable values obj contains all values corresponding to key group
  for (i=0; i<values.length; ++i) {
    for (var item in values[i]) {
        obj[item] = values[i][item];
  return obj;

In the above code, the result of the map.js code for your view will be
taken as the input, and for every key group (eg. "abc123"), it will
iterate through each row and do whatever string manipulations you
specify, and return the resulting object. The main takeaway is this:
values.length is the number of rows that match the keygroup being
handled, and the loop 'for (var item in values[i]' is what allows you
to access the values for each row i in the group of matching rows. Of
course, you'd need to modify this loop and the assigning of values to
obj (in my example) to suit your needs.

Again, I'm pretty sure this code would work, but I haven't really
tried it for large datasets, so I'd be interested to know if it does


You will probably need to write your own javascript function
On Fri, May 20, 2011 at 8:41 AM, Jim Klo <> wrote:
> I'm a little dumbfounded by reduce functions.
> What I'm trying to do is take a view that has heterogeneous values and union
> into a single object; logically this seems like what the reduce function
> should be capable of doing, but it seems I keep getting the reduce overflow
> error. Effectively I'm reducing the view by 50%.
> Consider the the simplistic scenario:
> doc A: { _id : "abc123", type:"resource", keyword:"nasa" }
> doc B: { _id : "abc123-timestamp", type: "timestamp", timestamp:
> "2011/05/19T12:00:00.0000Z", ref_doc: "abc123" }
> doc N: ....
> Doc A is the original doc... Doc B is the timestamp doc referencing Doc A
> via ref_doc field... Doc N is just another doc also referencing Doc A via
> ref_doc field.
> I can create a view that essentially looks like:
> Key Value
> ------------ ------------------
> "abc123" { .... doc A object .... }
> "abc123" { .... doc B object .... }
> "abc123" { .... doc N object .... }
> I would expect I could build a reduced view that looks something like this:
> Key Value
> ------------ ------------------
> "abc123" { .... merged doc .... }
> Ultimately this goes back to an issue we have where we need the node local
> timestamp of a document, without generating an event that would cause an
> update to doc A, causing it to get replicated. We figure we can store local
> data like a timestamp then join it back with the original doc via a view &
> list.
> Is there something magical about the reduce that's not well documented? Or
> maybe is there a better way to do this?  I know about using linked docs,
> were in my map function you can reference the _id of the linked document in
> the value you can return @ 1 - 1 merge with the include_docs=true, but don't
> think I can do that with N docs; or can I?
> Jim Klo
> Senior Software Engineer
> Center for Software Engineering
> SRI International

View raw message