-2

I have decided to have an interaction collection to hold the interactions between members of my App. Who blocked who, who liked who, who followed who, etc.

The schema will look something like this:

{
   mem_1: user_id,
   mem_2: user_id,
   mem_1_blocked_mem_2: "boolean",
   mem_2_blocked_mem_1: "boolean",
   etc...
}

The problem is, how is it decided, which member should be in the mem_1 field and which should be in the mem_2 field?

And then, when it comes to queries, how do I know which field is who?

1 Answers1

1

This might be the worst design I have seen ever.

I would suggest this:

{
   userId: "user1",
   blocked: ["user2", "user3"],
   liked: ["user5"]
},
{
   userId: "user2",
   blocked: ["user3"],
   liked: ["user1"]
}

If you like to speed up your design and if you don't care about redundancies, you can extend also by this:

{
   userId: "user1",
   blocked: ["user2", "user3"],
   liked: ["user5"],
   blocked_by: [],
   liked_by: ["user5", "user2"]
},
{
   userId: "user2",
   blocked: ["user3"],
   liked: ["user1"]
   blocked_by: ["user1"],
   liked_by: []
}
Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
  • I have considered the design in your answer. But it is expected that each user will interact with many many other users, causing their document to exceed 16mb. – Bear Bile Farming is Torture Jan 08 '23 at 18:53
  • Really? Assume each user has a size of 16 Bytes. This means a user needs to have 1 million(!) likes or blocks in order to reach this limit. This is really a lot, even for big apps like Facebook or Instagram. And based on this (and many others) questions, it rather looks like you have not written even a single line of code for your application but you already consider millions of users. – Wernfried Domscheit Jan 09 '23 at 09:02
  • Consider Youtube for example, it is not difficult to imagine a video getting millions of views, channel with millions of subscribers, etc. – Bear Bile Farming is Torture Jan 09 '23 at 09:04
  • In your design, if the document size exceeds 16MB, is it just a matter of adding another document in the same collection? – Bear Bile Farming is Torture Jan 09 '23 at 09:05
  • That would be no problem, of course. – Wernfried Domscheit Jan 09 '23 at 10:55
  • Let's suppose that there are 500k elements in the `blocked` list. Each time the user logs in, I need to display to them a list of suggested users. This means that I would need to check each suggested user against this 500k-length array. This would be really slow. How can this be made faster? Or is there an alternative design? – Bear Bile Farming is Torture Jan 09 '23 at 16:34
  • Calculate the list of suggested users offline, not when the user is connecting. And I really doubt any user would ever block 500k other users. And again, your application will start with a few users only. Maybe after a few years it may have a couple of thousand users. Don't waste too much time on that topic already today. – Wernfried Domscheit Jan 09 '23 at 16:50
  • "Calculate the list of suggested users offline". So I would need to store this list for every user on some other collection? Do you believe that's would Apps like YouTube does with their suggested videos? – Bear Bile Farming is Torture Jan 09 '23 at 16:56
  • Actually, it is not difficult to imagine a blocked list becoming large quickly. Imagine a dating app like Tinder. It is expected that the user will either block or like a suggestion everytime. – Bear Bile Farming is Torture Jan 09 '23 at 18:26
  • Do you seriously compare your (not yet existing app) with giant apps like YouTube or Tinder? You should be little more realistic. – Wernfried Domscheit Jan 09 '23 at 19:56
  • But if possible, why shouldn't I design my schema the right way now instead of having to refactor it when I get to a certain scale? – Bear Bile Farming is Torture Jan 09 '23 at 20:04
  • btw, could you have a look at https://stackoverflow.com/questions/75054458/how-to-use-a-variable-defined-in-let-as-property-name-of-an-object-field-in-the to see if a solution is possible? – Bear Bile Farming is Torture Jan 09 '23 at 20:06
  • https://www.mongodb.com/community/forums/t/how-to-use-a-variable-defined-in-let-as-property-name-of-an-object-field-in-the-pipeline-in-a-lookup/207769/4 here is a more detailed conversaition on the same problem – Bear Bile Farming is Torture Jan 09 '23 at 20:06