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


#1

I have the following view :
function (doc, meta) {
if (meta.type == “json”)
if ( doc.id
&& doc.id.root.indexOf(“1.2.250.1.199.16.1.1”) == 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
wound2QualificationRev1

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

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

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 ?


#2

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).

http://docs.couchbase.com/admin/admin/Views/views-writing.html#reduce-functions


#3

@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 ?


#4

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