how to do joins in couchbase using java code

Hi ,
I need to find mutual friends from the documents .How to do this by using java.

It will be easier to help you if you provide some sample documents.

Generally speaking you do not have "joins" using queries, so you have to work with the design of your docs.
So please give us some examples.

Hi ,
I am new to couchbase.I don't have document design idea in couchbase.But give me suggestions for this document design for users with finding mutual friends . My sample design is like this for users .For each user with different document like this
user1:
{
"email": "user1@gmail.com",
"name": "user1",
"location": "india"
}
user2:
{
"email": "user2@gmail.com",
"name": "user2",
"location": "india"
}

For each user diffrent friend list docuemnt like this

user1_Friendlist
{
"uids": "user2@gmail.com,user3@gmail.com"
}

user2_Friendlist
{
"uids": "user1@gmail.com,user3@gmail.com"
}

By above documents how to we know user3 is mutual friend to both of user1 and user2 . How to implement code for this by using java code. If you have any design document idea about this please give me some suggestions . I am new to couchbase .

1 Answer

« Back to question.

Hello,

I am thinking about 2 different approaches.

Option 1:
You use key lookup to get the friends of user 1 and friends of user 2 then you look if they have some friends in common from your Java code. So in this case you start from the users and compare all friends.

Option 2:
You create a design document that emits friends, for each user. They you call this view, using a friends ID to see if other users have the same friends. Or you can group by user and count the number of friends that they have.

So for this I have changed a little your JSON document to have a real list of JSON string in the friends list, something like

user2_Friendlist
{
"uids": ["user1@gmail.com","user3@gmail.com"]
} 

then I have created a view with the following map function:

function (doc, meta) {
  if (doc.uids) {
    for (var fId in doc.uids) {
      emit( doc.uids[fId], 1 );
    }  
  }
}

So now you can for example get the list of friends for user3 and see if they have the same users...

friends?key="user3@gmail.com"

This is just a sample, you have to see how to build the view and query based on your exact business use case

PS: sorry for my late answer, I do not why I have not received notification for your comment.

Regards
Tug
@tgrall