2
votes

I want to design User & Friend relationship in OrientDB. I have 1 Vertex User & 1 Edge Friend. Here I do in the console

create class User extends V
create class Friend extends E

I create 10 User (from User_0 until User_9)

create vertex User set name='User_0'
create vertex User set name='User_1'
create vertex User set name='User_2'
create vertex User set name='User_3'
create vertex User set name='User_4'
create vertex User set name='User_5'
create vertex User set name='User_6'
create vertex User set name='User_7'
create vertex User set name='User_8'
create vertex User set name='User_9'

Then I create the edge Friend

create edge Friend from #11:0 to #11:1
create edge Friend from #11:0 to #11:2
create edge Friend from #11:0 to #11:3
create edge Friend from #11:0 to #11:4
create edge Friend from #11:0 to #11:5
create edge Friend from #11:1 to #11:0
create edge Friend from #11:1 to #11:2
create edge Friend from #11:1 to #11:3
create edge Friend from #11:1 to #11:4
create edge Friend from #11:1 to #11:5
create edge Friend from #11:1 to #11:6

First question: If User_0 friend with User_1, do we need to store both of them in Edge? In example above, I store user #11:0 to #11:1, and #11:1 to #11:0 (2 Edge). I do this in traditional database (MySQL), I want to know the best practice in NoSQL (OrientDB that I use now). Or just save with 1 row for user #11:0 to #11:1 (1 Edge only).

Second, how I get all the friends of User_0? Provide sql select example, please. I want the result out array of user @RID & other fields of friend of User_0. So the query will be same for all User to get all friends (if we design Friend edge with 2 row Edge, #11:0 to #11:1 and #11:1 to #11:0).

If the design is 1 row Edge for both of Friend, it will be different, the query for User_0 how, and User_1 how to get all friends. But skip this if we store 2 friend relationship in 2 row of edge).

Third question, How I can get my friend mutual friends (that means the friends of my friend are my friends too). So, User_0 if see User_1 friends, the mutual friends are #11:2,#11:3,#11:4,#11:5. #11,6 is not mutual friend User_1 of User_0.

I think sample just 6 or less user is enough, but I already created 10 users.

Thanks!

1
wow... none don't know? - Jef
Have I answered your question ? - AlexB
see my comment below your answer :) - Jef

1 Answers

1
votes

Your data model is fine. Two user vertices and a friend edge is enough to achieve this. It will take less storage.

I would use a linklist to store the friend request received and sent in each users. A User has multiple friendrequest.

Document API :

traverse IsFriendsWith from #10:1234 while $depth <= 3 strategy BREADTH_FIRST

Graph API :

    select from (
      select 
        expand(unionall(
          outE("IsFriendsWith").inV(), 
          inE("IsFriendsWith").outV()
        ))
        from (
          select 
              expand(unionall(
                outE("IsFriendsWith").inV(), 
                inE("IsFriendsWith").outV()
              ))
          from (
             select expand($u) let $u = first((select from User where uuid = "1"))
          )
        )
    )
  )

      where @rid <> $u and @rid NOT IN $u.both("IsFriendsWith")

Above query taken from this stackoverflow question.

Check out the social network domain in the documentation for more information.