Top N over aggregated (MapReduced)

Hi @alonspiegel,

I would think that N1QL would be the right fit, since you are already comfortable with SQL and you’ve already used it to describe your problem declaratively. If you’re having a performance issue with N1QL, you may want to discuss it on the N1QL forums (https://www.couchbase.com/forums/c/n1ql).

I’ve tried to figure out how to do this with Map/Reduce, and I think the source of the problem is the inability to sort the results by the values. So, using group_level=1, I can properly aggregate the counts (very simple with _count), but you’re right that you’d have to then sort them on the client side. If that count could somehow be added to the key, then sorting would be possible (you might have to add leading zeros, since I believe it sorts the keys lexically, and not numerically). There might be a trick I’m missing, but I don’t see a way to do that.