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

カテゴリを渡してキーを入力し、個別の行を取得できるように"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"]

次のクエリ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}
]}
于 2016-04-07T06:01:02.810 に答える