vendredi 22 juillet 2016

How to get documents with latest status in case some fields in one document does not existed in another document

I have some documents like following structure, I want to get the latest status document according with given doc.probNumber , but the key of the view would be the doc.userid. while userid is not existed in doc2

doc1: userid='abc',  probNumber='123', status='OPEN',...
doc2: probNumber='123', status='CLOSE'.....

I want to get merge or get two documents with latest status as CLOSE as following

userid='abc',  probNumber='123', status='CLOSE',...

Here is the view and reduce if I used probNumber as the key to emit, But how can I handle if I use userid as the key in the map to get the latest status ones with given same probNumber?

function(doc) {
  if (doc.probNumber) {
      emit(doc.probNumber, [doc.status]);
  }
}

  function(keys, values, rereduce) {
      var latest = values[0];
      var i = values.length;
      while (i--) {
        var val = values[i];
        if (val[0] == 'CLOSE') latest = val;
      }
      return latest;
    }

Aucun commentaire:

Enregistrer un commentaire