Alex
Alex

Reputation: 38529

Running couchdb view with group=false (SELECT DISTINCT type behavior)

I was following this guide on couchdb http://guide.couchdb.org/draft/cookbook.html#unique in order to return a distinct list from a view.

My map function looks like:

function(doc) {
  if(doc.PartnerName !=null) {
    emit(doc.PartnerName, null);
  }
}

And, I have a reduce function:

function(keys, values) {
  return true;
}

When I run this by hitting:

/dbName/_design/Partners/_view/my-view-name

I get this back:

{"rows":[
{"key":null,"value":true}
]}

If I add ?reduce=false to the end, I get back sort of the desired result:

{
  "total_rows":11,"offset":0, 
  "rows":[
    {"id":"a","key":"PARTNER_ONE","value":null},
    {"id":"b","key":"PARTNER_ONE","value":null},
    {"id":"c","key":"PARTNER_ONE","value":null},
    {"id":"d","key":"PARTNER_ONE","value":null},
    {"id":"e","key":"PARTNER_ONE","value":null},
    {"id":"f","key":"PARTNER_ONE","value":null},
    {"id":"g","key":"PARTNER_TWO","value":null},
    {"id":"h","key":"PARTNER_TWO","value":null},
    {"id":"i","key":"PARTNER_TWO","value":null},
    {"id":"j","key":"PARTNER_THREE","value":null},
    {"id":"k","key":"PARTNER_FOUR","value":null}
]}

However, I'm ideally trying to get a distinct list, so in the above example, it'd be PARTNER_ONE, PARTNER_TWO, PARTNER_THREE, PARTNER_FOUR

Upvotes: 0

Views: 165

Answers (1)

brdlph
brdlph

Reputation: 619

I think you are missing the group=true parameter. Try to query

/dbName/_design/Partners/_view/my-view-name?group=true

and see if that gives you the correct result.

Upvotes: 1

Related Questions