ddouglascarr
ddouglascarr

Reputation: 1442

couchDB reduce: does rereduce preserve the order of results from map?

With a couchdb view, we get results ordered by key. I have been using this to get values associated with a highest number. For example, take this result (in key: value form):

{1:'sam'}
{2:'jim'}
{4:'joan'}
{5:'jill'}

couchDB will sort those according to the key. (It could be helpful to think of the key as the "score".) I want to find out who has the highest or lowest score.

I have written a reduce function like so:

function(keys, values) {

var len = values.length;
return values[len - 1];

}

I know there's _stat and the like, but these are not possible in my application (this is a slimmed down, hypothetical example).

Usually when I run this reduce, i will get either 'sam' or 'jill' depending on whether descending is set. This is what I want. However, in large data-sets, sometimes I get someone from the middle of the list.

I suspect this is happening on rereduce. I had assumed that when rereduce has been run, the order of results is preserved. However, I can find no assurances that this is the case. I know that on rereduce, the key is null, so by the normal sorting rules they would not be sorted. Is this the case?

If so, any advice on how to get my highest scorer?

Upvotes: 0

Views: 113

Answers (1)

djc
djc

Reputation: 11731

Yeah, I don't think sorting order is guaranteed, probably because it cannot be guaranteed in clustered environments. I suspect the way you're using map/reduce here is a little iffy, but you should post your view code if you really want a good answer here.

Upvotes: 1

Related Questions