0

Using Nano to query a Couch view in NodeJS, to count the occurrences of tags in all documents. If I choose reduce in Fauxton on the view, it correctly displays with each tag name as the key, and the count. However, from my Node app, I just get 'Null' as the key, and a total of all tags.

The tag name (rather than the _id) is stored in each document. This is my map function (using the _count reduce function):

function (doc, meta) {
  if(doc.tags) {
    for(var i in doc.tags) {
      emit(doc.tags[i],1);
    }
  }
}

This is my (Nano) code in Node:

app.get('/listtags', function(req, res) {
    couch.view('objects', 'tags-count', {reduce: true}, function(err, body) {
        console.log(body);
    });
});

And it returns:

{ rows: [ { key: null, value: 8 } ] }

EDIT Adding example docs:

Tag:

{
"_id": "...",
"_rev": "1-...",
"type": "tag",
"name": "petrol"
}

Doc containing tags:

{
"_id": "...",
"_rev": "3-...",
"type": "car",
"title": "Audi",
"desc": "description here",
"tags": [
  "petrol",
  "aircon",
  "leather seats"
]
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user4893295
  • 533
  • 6
  • 25

1 Answers1

0

This is because you have to specify either the group_level option or the group=true option.

eg :

app.get('/listtags', function(req, res) {
    couch.view('objects', 'tags-count', {reduce: true, group: true}, function(err, body) {
        console.log(body);
    });
});
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Alexis Côté
  • 3,670
  • 2
  • 14
  • 30