How to return record(s) with max value in a view


I have the following view :
function (doc, meta) {
if (meta.type == “json”)
if (
&&“”) == 0
&& doc.setId
&& doc.versionNumber)
emit(doc.setId.extension, parseInt(doc.versionNumber.value))

That returns the following result set (WARNING PARTIAL):
Key Value
"4816f76e-33f9-456a-9e23-961799883994" 1

“4816f76e-33f9-456a-9e23-961799883994” 2

Now I would like to restrict the result set on the max value to return only
Key Value
"4816f76e-33f9-456a-9e23-961799883994" 2

Because the result set is partial, I could not use sort/limit to achieve my goal.
How would I modify the view ? With a custom reduce method ?


Without trying it, I think that the built-in _stats reduce function might give you what you need, at least so you can try it out. You could then write a custom reduce that was more efficient (i.e. it output only the max value, not all the other stats).


@matthew Where can I find the _stats reduce function source code ?
I have to say that it is a bit difficult for me to write such a function without debugging capabilities.
Is it possible to output debugging information from a reduce function ?


I found a CouchDB max reduce function that is working well.