Mapreduce 具有输入键的CouchDB中的Distinct

Mapreduce 具有输入键的CouchDB中的Distinct,mapreduce,couchdb,couchdb-nano,Mapreduce,Couchdb,Couchdb Nano,给定文件,如: [ {"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", "l

给定文件,如:

[
{"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"}
]
我希望能够传入类别和键入键并返回不同的行,例如传入
“cat1”
“type1”
的键并返回
[“line1”、“line2”]
或传入
“cat2”
“type1”
并返回
[“line2”、“line3”]

如果我没有传入密钥,那么就足够容易了:

地图

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

function(keys, values) {
  return null;
}
_count
我使用的是group:true,但在传递密钥时如何处理这个问题上遇到了难题

PS,使用node和nano so查询类似于:

db.view('catalog', 'lines', {key: ['cat1', 'type1'], group: true}, function (err, body) {
...
});
我希望能够传入类别和键入键并返回不同的行,即传入“cat1”和“type1”键并返回[“line1”、“line2”]或传入“cat2”和“type1”键并返回[“line2”、“line3”]

您可以通过使用正确的参数查询以下
map
reduce
来获得:

地图

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

function(keys, values) {
  return null;
}
_count
查询

对于“cat1”和“type1”:

对于“cat2”和“type1”:


为什么要将密钥包装在数组中?(即:
[doc.line]
而不仅仅是
doc.line
)还有,为什么还要麻烦使用reduce函数呢?我实际上在视图中传递了不止一个键:{key:['cat1',type1',group:true}-我使用group=true和reduce。这是我所看到的最佳实践。对于使用列表函数并在其中处理请求参数的组,需要减少