You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Javier Julio <jj...@gmail.com> on 2011/02/09 00:34:38 UTC

Query a view with id list but for one key

Hey all,

I've been reading on and using CouchDB since December last year. It's been
great and I've gotten my head around it quite well but on some harder tasks
I'm struggling a little. Let me talk about what I'm currently doing.

I'm using CouchDB for a social app where users can save places. The same
place can be stored in the db multiple times since its tied to a user. Each
place has a unique id. Since this is a social app, say if User A has various
friends and they have saved the same place, I'd like have a view where I can
get a list of those friends. Think like in Facebook when you have a comment
or status update and a bunch of people "like" it, you'd see something like
"You and 5 others like this". When you click on that link you'll get a list
of those people's names.

I can easily create a view that has a map function that just emits the
document id, place id, and user id. The problem I have figuring out is if I
have an id list of User A's friends how can I pass that into the view? From
what I know this doesn't seem like how it works. I'm most likely approaching
this wrong and thinking to much about how I would do this with SQL
(basically using a WHERE IN statement).

Any suggestions? While I just could have the view return all places saved by
anyone else but that seems excessive because there is definitely the
potential that many people have that place saved that aren't even that
users' friend.

Ciao!
Javi

-- 
Javier Julio
Senior Software Developer, 9mmedia
www.javier-julio.com

Re: Query a view with id list but for one key

Posted by Javier Julio <jj...@gmail.com>.
Apologies if what I sent wasn't clear. I was concerned with describing the
situation rather than giving some good examples. Let me try to rectify that
as I'm stuck on how to go about solving this. Say I have User A, B, C, D,
and E.

User A, B, and C have saved the same place, Place 1, and are friends. Each
place saved by a user is a single document. Note that each place saved is a
single document with the associated user so there can be multiple documents
of the same place but for tied to a different user.

When User A is looking at Place 1, I want to retrieve his/her other friends
that have also saved the same place.

I then have a view called "places-by-friends" that has a key of placeId and
userId. So in the view I would be having an emit() call of:

emit([doc.placeId, doc.userId], doc.fullName)


So I have the list of user id's for User A's friends, how would I use that
against that view? I'm doing something wrong since basically I want to look
against multiple user id values based on a list. This is not how it works
from what I understand. I basically want to have a result just including the
place documents from the users B and C since they are friends with User A
but not user D and E, those are left out.

I could just make a request to the view for each friend id that user has but
that seems excessive especially if they can have lots of friends. There has
to be something I'm missing here. Any suggestions? Maybe pointers on a
different approach?


On Tue, Feb 8, 2011 at 6:34 PM, Javier Julio <jj...@gmail.com> wrote:

> Hey all,
>
> I've been reading on and using CouchDB since December last year. It's been
> great and I've gotten my head around it quite well but on some harder tasks
> I'm struggling a little. Let me talk about what I'm currently doing.
>
> I'm using CouchDB for a social app where users can save places. The same
> place can be stored in the db multiple times since its tied to a user. Each
> place has a unique id. Since this is a social app, say if User A has various
> friends and they have saved the same place, I'd like have a view where I can
> get a list of those friends. Think like in Facebook when you have a comment
> or status update and a bunch of people "like" it, you'd see something like
> "You and 5 others like this". When you click on that link you'll get a list
> of those people's names.
>
> I can easily create a view that has a map function that just emits the
> document id, place id, and user id. The problem I have figuring out is if I
> have an id list of User A's friends how can I pass that into the view? From
> what I know this doesn't seem like how it works. I'm most likely approaching
> this wrong and thinking to much about how I would do this with SQL
> (basically using a WHERE IN statement).
>
> Any suggestions? While I just could have the view return all places saved
> by anyone else but that seems excessive because there is definitely the
> potential that many people have that place saved that aren't even that
> users' friend.
>
> Ciao!
> Javi
>
> --
> Javier Julio
> Senior Software Developer, 9mmedia
> www.javier-julio.com
>



-- 
Javier Julio
Senior Flex Developer, 9mmedia
www.javier-julio.com