1

Given documents like:

[
{"id":1, "category": "cat1", "type": "type1", "line": "line1"},
{"id":2, "category": "cat1", "type": "type1", "line": "line1"},
{"id":3, "category": "cat1", "type": "type2", "line": "line1"},
{"id":4, "category": "cat1", "type": "type1", "line": "line2"},
{"id":5, "category": "cat2", "type": "type1", "line": "line2"},
{"id":6, "category": "cat2", "type": "type1", "line": "line3"}
]

I want to be able to pass in the category and type keys and get back the distinct lines e.g. pass in keys of "cat1" and "type1" and get back ["line1", "line2"] or pass in "cat2" and "type1" and get back ["line2", "line3"]

Easier enough if I am not passing in keys:

map

function(doc) {
    emit([doc.line]);
}

reduce

function(keys, values) {
  return null;
}

I am using group: true, but stumped on how to handle this when passing in keys.

PS, using node and nano so query looks similar to:

db.view('catalog', 'lines', {key: ['cat1', 'type1'], group: true}, function (err, body) {
...
});
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Samuel Goldenbaum
  • 18,391
  • 17
  • 66
  • 104

1 Answers1

1

I want to be able to pass in the category and type keys and get back the distinct lines i.e. pass in keys of "cat1" and "type1" and get back ["line1", "line2"] or pass in "cat2" and "type1" and get back ["line2", "line3"]

You can get that by querying the following map and reduce with the right parameters:

map

function(o) {
  emit([o.category, o.type, o.line]);
}

reduce

_count

queries

For "cat1" and "type1":

/mydb/_design/mydesign/myview?group=true&startkey=["cat1","type1"]&endkey=["cat1","type1",{}]

{"rows":[
{"key":["cat1","type1","line1"],"value":2},
{"key":["cat1","type1","line2"],"value":1}
]}

For "cat2" and "type1":

/mydb/_design/mydesign/myview?group=true&startkey=["cat2","type1"]&endkey=["cat2","type1",{}]

{"rows":[
{"key":["cat2","type1","line2"],"value":1},
{"key":["cat2","type1","line3"],"value":1}
]}
Aurélien Bénel
  • 3,775
  • 24
  • 45