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

此生再无相见时 提交于 2019-12-13 02:27:38

问题


I have some documents like following structure, userid in doc1 is not existed in doc2,

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

I want to get the latest status document according with the given doc.probNumber , But the key of the view would be doc.userid Or use doc.userid with doc.probNumber combination to achieve on that following

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

Here is the view and reduce if I used probNumber as the key to emit, But my client request me use userid as the key in the map to get the latest status ones with given same probNumber? if not, is it possible use doc.userid with doc.probNumber combination 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;
    }

来源:https://stackoverflow.com/questions/37814311/how-to-get-documents-with-latest-status-in-case-some-fields-in-one-document-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!