1

给定文件,如:

[
{"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"}
]

我希望能够传入类别并键入键并取回不同的行,例如传入 and 的键"cat1""type1"返回["line1", "line2"]或传入"cat2""type1"返回["line2", "line3"]

如果我传递密钥,那就更容易了:

地图

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

减少

function(keys, values) {
  return null;
}

我正在使用 group: true,但在传递密钥时如何处理这个问题却很困惑。

PS,使用节点和纳米所以查询看起来类似于:

db.view('catalog', 'lines', {key: ['cat1', 'type1'], group: true}, function (err, body) {
...
});
4

1 回答 1

1

我希望能够传入类别和类型键并取回不同的行,即传入“cat1”和“type1”的键并取回[“line1”,“line2”]或传入“cat2”和"type1" 并返回 ["line2", "line3"]

您可以通过查询以下内容mapreduce使用正确的参数来获得它:

地图

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}
]}
于 2016-04-07T06:01:02.810 回答