Prakash Thapa
Prakash Thapa

Reputation: 1285

How to access json value inside reduce function in couchbase?

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.

Upvotes: 2

Views: 385

Answers (1)

user1697575
user1697575

Reputation: 2848

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

Upvotes: 3

Related Questions