couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adam Kocoloski <>
Subject Re: View to fetch a list of conversation id's with recent activity
Date Wed, 24 Apr 2013 20:24:39 GMT
If the number of unique thread_ids in a given result set is not huge you might consider a _list
function that suppresses rows where the thread_id matches one that you've already sent out
the wire.


On Apr 22, 2013, at 8:49 PM, Michael Jackson <> wrote:

> I'm building a messaging app where messages have a thread_id (think email
> threads). When messages are delivered, they are denormalized so that each
> user gets their own copy of that message with his/her user_id attached. So
> I end up with a bunch of documents like this:
> {
>  created_at: '...',
>  message: '...',
>  thread_id: 123,
>  user_id: 456
> }
> I'd like to be able to easily fetch a list of conversations with recent
> activity for a given user id. This could just be a list of thread_ids. I've
> been using the following map function:
> function (doc) {
>  emit([ doc.user_id, doc.created_at ], doc.thread_id);
> }
> This gives me a time-ordered list of threads with recent activity, but it
> has a bunch of duplicate ids in it. I've tried throwing the results of the
> map into a reduce function that creates an array of unique values, but then
> I lose the time-ordered guarantee.
> My next thought was to emit the created_at date in the map as well so I
> could order by time in the reduce, like this:
> function (doc) {
>  emit([ doc.user_id, doc.created_at ], { created_at: doc.created_at,
> thread_id: doc.thread_id });
> }
> Then, in the reduce I could build up a hash of thread_ids to the most
> recent created_at date for that id and just let the client build an array
> of sorted ids using the value, but it feels a little clunky.
> Can anyone think of a more elegant way to solve this problem? Many thanks
> in advance!
> --
> Michael Jackson
> @mjackson

View raw message