给定的文档,如:
[
{"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(keys, values) {
return null;
}
我正在使用group:没错,但在传递密钥时,我对如何处理这个问题感到困惑。
PS,使用node和nano,所以查询看起来类似于:
db.view('catalog', 'lines', {key: ['cat1', 'type1'], group: true}, function (err, body) {
...
});
我希望能够传入类别和类型键并返回不同的行,即传入"cat1"one_answers"type1"的键并返回["line1"、"line2"]或传入"cat2"one_answers"type 1"并返回["line2"、"line3"]
您可以通过使用正确的参数查询以下map
和reduce
来获得:
地图
function(o) {
emit([o.category, o.type, o.line]);
}
降低
_count
查询
对于"cat1"one_answers"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}
]}
对于"cat2"one_answers"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}
]}