2

my reduce function in Couchbase[just for testing] :

function(keys,values,reduce){
return values[0];
}

The result is here

{"rows":[
{"key":null,"value":{"doctype":"closed_auctions","buyer":"person14108"}}
]
}

I would like to get individual value inside reduce for further process so when I try to get value of doctype like values[0].doctype it returns null even though there should be "closed_auctions". what is the problem? How can I get individual value(I mean field value) inside reduce function.

Prakash Thapa
  • 1,285
  • 14
  • 27

1 Answers1

3

Your reduce function called after map performed. Also remember that re-reduce also called and at that time your result is further aggregated. You should use "group=true" when running this view.

So, sometimes values[0] might not have document

You need to check for existence of the property/doc first, e.g.

function(keys, values, rereduce)
{
  if (values[0] && values[0].doctype)
  {
    return values[0].doctype;
  }
}

Hope that helps.

See also: 9.5.2.4. Writing Custom Reduce Functions

user1697575
  • 2,830
  • 1
  • 24
  • 37