Link between documents


#1

Hi,

I have interlinked two documents say User profile(main document) with his friends list(sub document).
User_01:
{
“name”:“jack”,
“age”:23,
“friends_list”:["friend_01,…]} and
friend_01:{…}.

Now, I have to retrieve data using GET METHOD(JAVA)…I tried using GETBULK but dont know how to use the equivalent for:

beer = client.get(“beer:A_cold_one”);
comments = client.multiGet(beer.comment_ids)
(http://www.slideshare.net/Couchbase/couchbase-app-development-documents-schemas-and-their-relationships)
in java???


#2

I would suggest that. You just add the list of friends into the document as an Array. From there you can map/reduce to find particular friends.


#3

Thanks for your reply. But am new to map/reduce function. If i insert my friends list in same document(it will be an performance issue)? bcoz my friends list will be keep on growing. So, i thought keeping it in separate document will be good…Any suggestions pls.


#4

so I’ll break it down into two parts 1. Sample Json of user and friends. 2 . basic map/reduce job.


#5

So we “Sara Tiller” and she has 2 friends… Mark Smith , and Ted Rogers.

{
“UserName”:“Sara Tiller”,
“Friends”:[“Mark Smith”,“Ted Rogers”]
}


#6

2.a Now the Map/reduce…
We want to make sure that the document has a UserName and that the array is not empty

function(doc,meta){ if(doc.UserName && doc.Friends.length >0)

}


#7

2.b looping through the list/array of friends and making an index(i.e. Map/reduce)

function(doc,meta){ if(doc.UserName && doc.Friends.length > 0){ for( var x in doc.Friends){ emit(doc.UserName,x); } } }

#8

The output of this will look something like this.
127.0.0.1:8092/test_db/_design/friends/_view/find_friends?key=“Sara Tiller”&reduce=false

{
“total_rows”:1,
“rows”:[
{“key”:“Sara Tiller”,“value”:“Mark Smith”},
{“key”:“Sara Tiller”,“value”:“Ted Rogers”},
]
}


#9

Thank you for your valuable comment. Now, i can able to retrieve Friends list of a user(You gave clear idea) . I want to know

  1. Whether I can be able to retrieve friends list of two particular users(Sending parameter(username) to map function from sdk)
  2. Or I have to write two views for each user to get their friends list.
    Am trying to find mutual friends between two users. So I have to get friendslist of two users and have to compare it. Any Suggestions or idea???

#10

You can pass mutiple keys to get your result like below.
http://127.0.0.1:8092/test_db/_design/friends/_view/find_friends?keys=[“Sara Tiller”,“Jason Cribs”]&reduce=false .

To get more examples go to http://www.couchbase.com/docs/couchbase-manual-2.1.0/couchbase-views-writing-querying-selection.html