I would like to select a single document based on its timestamp, I want the most recent one. So I think there are 2 ways to easily solve this, and was wondering if there were any efficiency differences between them.
Firstly I create a view with a map function that looks something like this:
function (doc) {
if(doc.type === 'foo') {
emit(doc.timestamp, null);
}
}
As views are stored by the natural order of the keys… One option would be to add a reduce function, that just selects the last value. Something like this:
function(keys, values, rereduce) {
return values.length === 0 ? null : values[values.length - 1];
}
Alternatively I could just query the view using descending = true
and limit = 1
in order to pick out the last, and therefore latest document.
Is one of these approaches preferable? Is there a better 3rd option?