You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Travis LaDuke <tr...@gmail.com> on 2010/02/25 00:34:04 UTC

Order posts by most recently commented on?

Hello,
I'm trying to make a very simple forum, which is like a very simple
blog, except anyone can start a topic. The other small difference is
that I need to list topics in order of when they were last replied to
(commented on) instead of when the topic was created. You know how a
forum works. I can't figure out any combination of map/reduce/list
that works. Is it possible? I want to try to make this a standalone
couchdb app.

thanks
--Travis

Re: Order posts by most recently commented on?

Posted by Adam Wolff <aw...@gmail.com>.
You'd have a two-step "submitComment" process, where you first submit the
comment, then go retrieve and update the topic document. Depending on what
else you might do to the topic document, you could possibly ignore conflicts
when you try to update the topic document -- by definition it's been
updated.

A

On Wed, Feb 24, 2010 at 4:52 PM, Travis LaDuke <tr...@gmail.com>wrote:

> On Wed, Feb 24, 2010 at 4:12 PM, Jens Alfke <je...@mooseyard.com> wrote:
> > You could add a "lastCommentDate" property to each topic's document, and
> > update it whenever you add a comment. You'll need to resolve conflicts if
> > you try to update the date but someone else just updated it before you
> did.
> >
>
> How would I go about updating the topic? Just have javascript submit
> the update after it gets a success on the comment creation? Seems
> fragile. Can the new _update thing do it? It doesn't look like it...
>

Re: Order posts by most recently commented on?

Posted by Travis LaDuke <tr...@gmail.com>.
On Wed, Feb 24, 2010 at 4:12 PM, Jens Alfke <je...@mooseyard.com> wrote:
> You could add a "lastCommentDate" property to each topic's document, and
> update it whenever you add a comment. You'll need to resolve conflicts if
> you try to update the date but someone else just updated it before you did.
>

How would I go about updating the topic? Just have javascript submit
the update after it gets a success on the comment creation? Seems
fragile. Can the new _update thing do it? It doesn't look like it...

Re: Order posts by most recently commented on?

Posted by Jens Alfke <je...@mooseyard.com>.
On Feb 24, 2010, at 3:34 PM, Travis LaDuke wrote:

> I need to list topics in order of when they were last replied to
> (commented on) instead of when the topic was created. You know how a
> forum works. I can't figure out any combination of map/reduce/list
> that works. Is it possible?

You could add a "lastCommentDate" property to each topic's document,  
and update it whenever you add a comment. You'll need to resolve  
conflicts if you try to update the date but someone else just updated  
it before you did.

—Jens

Re: Order posts by most recently commented on?

Posted by Adam Wolff <aw...@gmail.com>.
Couch just doesn't handle this well. You can either reduce to the last
comment date for each topic and sort application-side, or you can go update
the topic document. One good thing if you choose the latter: if you
stipulate that updating the topic always updates the "lastCommentDate," then
you can ignore write failures due to outdated revs.

A

On Wed, Feb 24, 2010 at 4:49 PM, Travis LaDuke <tr...@gmail.com>wrote:

> On Wed, Feb 24, 2010 at 4:24 PM, David Goodlad <da...@goodlad.ca> wrote:
> > If you have comments stored as individual docs, with an associated
> > topic doc on each, you could use a map function like:
> >
> > function(doc) {
> >  if(doc.type == 'comment') {
> >    emit(doc.post_time, {"id": doc.topic_id});
> >  }
> > }
> >
> > You could then query that view with include_docs=true to get all the
> > topics sorted by comments' post times. You'd probably want a list
> > function that would remove duplicates, since each topic would be
> > included in the view once per comment in that topic.
> >
>
> Thanks, I didn't know about include_docs. I had thought of this before
> (without the include_docs), but gave up when I thought it would make
> it hard to paginate. If I say like limit 10, and there's a thread with
> 10 comments, I'll only get one topic in the list. Is there a way?
>

Re: Order posts by most recently commented on?

Posted by Travis LaDuke <tr...@gmail.com>.
On Wed, Feb 24, 2010 at 4:24 PM, David Goodlad <da...@goodlad.ca> wrote:
> If you have comments stored as individual docs, with an associated
> topic doc on each, you could use a map function like:
>
> function(doc) {
>  if(doc.type == 'comment') {
>    emit(doc.post_time, {"id": doc.topic_id});
>  }
> }
>
> You could then query that view with include_docs=true to get all the
> topics sorted by comments' post times. You'd probably want a list
> function that would remove duplicates, since each topic would be
> included in the view once per comment in that topic.
>

Thanks, I didn't know about include_docs. I had thought of this before
(without the include_docs), but gave up when I thought it would make
it hard to paginate. If I say like limit 10, and there's a thread with
10 comments, I'll only get one topic in the list. Is there a way?

Re: Order posts by most recently commented on?

Posted by David Goodlad <da...@goodlad.ca>.
On Thu, Feb 25, 2010 at 10:34 AM, Travis LaDuke <tr...@gmail.com> wrote:
> Hello,
> I'm trying to make a very simple forum, which is like a very simple
> blog, except anyone can start a topic. The other small difference is
> that I need to list topics in order of when they were last replied to
> (commented on) instead of when the topic was created. You know how a
> forum works. I can't figure out any combination of map/reduce/list
> that works. Is it possible? I want to try to make this a standalone
> couchdb app.

If you have comments stored as individual docs, with an associated
topic doc on each, you could use a map function like:

function(doc) {
  if(doc.type == 'comment') {
    emit(doc.post_time, {"id": doc.topic_id});
  }
}

You could then query that view with include_docs=true to get all the
topics sorted by comments' post times. You'd probably want a list
function that would remove duplicates, since each topic would be
included in the view once per comment in that topic.

Dave