couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Davis <>
Subject Re: need help for a query
Date Thu, 21 May 2009 23:57:54 GMT
I don't think you can do that in 1 query because you have two
unrelated orderings. Remember, don't be too concerned with multiple
queries to the database.

The layout I'd use is this:

bar View:
Map: emit(doc.user_id, doc.created_at)
Reduce: return max(values); // You'll have to write max

zing View 2:
Map: emit([doc.user_id, doc.created_at], doc.post_id)

Query strategy:
Multi-get the bar view with your user id's of interest.
top20 = []
For each user:
    get last 20 posts for user
    merge last 20 into top20

There could be something more clever but nothing is coming right now.

Paul Davis

On Thu, May 21, 2009 at 7:39 PM, Patrick Aljord <> wrote:
> Hey all,
> I have a blog with many posts and many comments done by many users, a
> comment has a post_id, a user_id and a created_at holding the creation
> date of the comment.
> I would like to query the 20 latest comments done only by user X, Y
> and Z together, that is to say, if user X did the 20 latest comments
> then it should return those 20 latest comments done by x only, but if
> not, it should return for example something like 10 comments done by
> X, 7 comments done by Y and 3 by Z, I hope you get the idea. And I
> want them to be ordered by created_at.
> I don't think there is a way to do so with one query only. What would
> be the best way to do this query with couchdb?
> Thanks in advance,
> Pat

View raw message