1

Consider a database with documents that looks something like:

{ 
   username: "user_1",
   email: "user_1@mail.com"
}

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

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

And I return a list of their emails:

["user_1@mail.com","user_2@mail.com","user_N@mail.com"]

How can I solve this issue efficiently?

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

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
NadavL
  • 390
  • 2
  • 12

2 Answers2

3

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;
  })
})
the4lt
  • 121
  • 5
1

You can create a view on the database that maps the username to the email, and then run db.view with the keys as parameters to return your results.

Alternatively, you can also use the node library by Cloudant to retrieve documents with Cloudant Query or Cloudant Search.

Views are likely all that you need, but if you need to query based on different combinations of properties in each document, search indexes with Cloudant search may be the way to go. I'm not sure what benefits Cloudant query provides.

Rho
  • 370
  • 2
  • 10