问题
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