How would you store user friends?

Hello,

how would you store user friends in your database schema ?

example 1 (Collection User)

{
  user: {
     name: string;
     lastname: string;
     friends: [
        "uuid1",
        "uuid2
     ]
  }
}

option 2 for each friend a document
Collection User
Collection Friends

The problem in option 1 is when you have example 300k followers/friends
then your document is more than 20mb, than you reached the limit size.

Is option2 right or is there another (better) way ?

There is invariably no single “right” way for every situation.

What about having “user” and “relationship” ? All users have a record in “user” and each relationship document is simply a pair of user IDs and the “type” of relationship?

Even if you’re only modelling one relationship (i.e. friends) then the relationship can automatically be bi-directional (depending on how you store the two IDs and how you query). If you were to store IDs like your option 1, then you could easily end up with A being friends with B, but B not being friends with A.

But this all basic database design and there are a lot better resources out there discussing this I’m sure.

HTH.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.