Can I call 'emit' multilple time in the map function?


#1

Let assuming if I store all kind of object in a same bucket.
And assumming that I need to separate user, login_logs, and token in different object:
Because CouchBase document say that I should not have to many views, and I want to query these:

  • Check if email and password correct.
  • Find the user with that token, if the token not expire yet.
  • List all login history of an user order by login time.

Example login_log object:

{
  "type": "login_logs",
  "createdOn": "JSON iso string time",
  "user_id": "<guuid>",
  "device": "sone string"
}

Example token object:

{
  "type": "token"
  "id": "<the token string>", // the meta id
  "exprire": "JSON iso string",
  "user_id": "<uuid>",
  "device": "some string"
}

And if I have ONLY ONE map functiion that:

function(doc, meta) {
  switch(doc.type) {
    case "token":
      emit([meta.id, dateToArray(doc.exprire)], doc.user_id);
      break;
    case "login_logs":
      emit([dateToArray(doc.createdOn), doc.user_id]);
      break;
    case "user":
      for(i = 0; i<doc.emailes.length; i++) {
        if(doc.emailes[i].confirmed)
          emit(doc.emailes[i].address, doc.password);
      }
      break;
  }
}

Will this affected performance? Disk is cheap, I more care about speed (RAm and CPU more expensive I think)


#2

Emitting more than once in a map function is allowed and also encouraged if it fits your use case.