给出如下文件:
[
{"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:true,但是在传入密钥时难以理解如何处理它。
PS,使用node和nano所以查询看起来类似于:
db.view('catalog', 'lines', {key: ['cat1', 'type1'], group: true}, function (err, body) {
...
});
答案 0 :(得分:1)
我希望能够传入类别和键入键并返回不同的行,即传入“cat1”和“type1”的键并返回[“line1”,“line2”]或传入“ cat2“和”type1“并返回[”line2“,”line3“]
您可以通过使用正确的参数查询以下map
和reduce
来获取该信息:
<强>地图
function(o) {
emit([o.category, o.type, o.line]);
}
<强>减少强>
_count
<强>查询强>
对于“cat1”和“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”和“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}
]}