1

I am using nodejs's nano npm module and couchdb to get the total number of documents based on a status message that I should be able to pass in a couchdb view.

The view looks like the following.

{
    _id: "_design/docCount",
    views: {
      fileCountByStatus: {
        reduce: "_count",
        map:
          'function (doc) {\n  if(doc.status === "COMPLETE") {\n    emit(doc._id, 1);\n  }\n}'
      },
    },
    language: "javascript"
  }

I am accessing the above view using nano's view function.

My question is, is there anyway I can pass the doc status using the view function instead of hardcoding it (COMPLETE in the above case)

Phantom007
  • 2,079
  • 4
  • 25
  • 37

1 Answers1

0

Answering my own question.

The general case is to create an index on status with a map function like this:

function(doc) {
  emit(doc.status, 1)
}

Then you can query the view for any value of status

db.view('mydesigndoc', 'myview', { key: 'complete' })
// or
db.view('mydesigndoc', 'myview', { key: 'new' })
Phantom007
  • 2,079
  • 4
  • 25
  • 37