Querying Reduce views (limiting and sorting data)


#1

Hi,

I am working on a comments system in couchbase. I have written a custom view using map and reduce functions to group related comments by conversation id and order them by time.

e.g.

map(doc){
   emit(doc.conversationId, doc)
}

reduce(key, values){
   values.sort(function(a,b){
       return a.date - b.date;
   }
   return values;
});
}

When I look at the view I now have {key: conversation1, values: [comment1, comment2, comment3]} etc. What I want to do is filter by key and then limit to 10 values, however when I limit it is applying the limit to the number of keys returned and not the values. In fact the values do not matter at all.

It begs the question, what is the point of the reduce function as in this case I can only limit and sort based on key (map only). The kicker is that I can’t group data without having a reduce function implemented.

Could someone explain how I might be able to achieve my requirement of being able to query for a particular conversation and retrieve comments in date order. I want to be able to use limit and skip to only load comments as someone scrolls, so order does matter. If I do resort to map only I have to do the heavy lifting at the front end and I can’t guarantee order.

Thanks

Simon