Search code examples
node.jscouchdbcloudantcouchdb-nano

Query Cloudant (using the node.js module) database for a list of documents?


Consider a database with documents that looks something like:

{ 
   username: "user_1",
   email: "[email protected]"
}

Now, I'm given a list of usernames, i.e:

["user_1", "user_2", "user_N"]

And I return a list of their emails:

["[email protected]","[email protected]","[email protected]"]

How can I solve this issue efficiently?

Using Node.js to query a Cloudant (based on the nano library) database.


Solution

  • You don't need a new view, see HTTP Bulk Document API

    Nano provides db.fetch method, so

    db.fetch({keys: ["user_1", "user_2", "user_N"]}, function(err, body) {
      var emails = body.rows.map(function(row) {
        return row.doc.email;
      })
    })