Suggestions on level based referral schema structure

Where should one focus when building a referral system (let’s say 15 level referral). How to make schema more effective yet simple to understand?

1 Like

Hi @Ankit_Joshi,

I am not sure I understand what data will the system store?

Do you have users which will have a referral list? Do you need a link as deep as 15 levels? Thanks
Pavel

Hi @Pavel_Duchovny ,

Suppose I am building a referral system. My referee used my referral code so he is my level one referee. Other user used my level 1 referee’s referral code, so he’ll be my level 2 referee and so on. I need to link as deep as 50 levels. What should be the most efficient way to do this.

Hi @Ankit_Joshi,

Well in this case I would try three designs:

  1. Tree model:
{ userid : ... ,
  level1 : {
     referedUsers : [ userid1, userid2 ...],
     level2 : {
     referedUsers : [ userid3, userid4 ...],
     ....
                 { level50 : ... }
}
}
  1. Field based:
{ userid : ... ,
 level1 : [ userid1, userid2 ...],
level2 : [ userid3, userid4 ...],
...
level50 : ...
}
  1. Reference pattern:
    Users collection:
{ userid : ... ,
  levelRefferals : [ {level : 1 , referenceListId : ...} ,{ level: 2 , referenceListId : ...} ...],

Referral Lists collection:

{
_id : ...,
referrals : [ userid1, userid2, ... ]
}

The one to choose is based on your application way of searching and presenting and updating data.

Best
Pavel

1 Like

Hi @Pavel_Duchovny ,

I am going with this structure:

{
    referrer: userId,
    referee: [
        { level: 1, refereeList: [ userId1, userId2, .... ] },
        { level: 2, refereeList: [ userId7. userId8, .... ]},
        ....
    ]

Will this be any good?

Best
Ankit

Hi @Pavel_Duchovny ,

Regarding the Field based model you described. Do I have to create static level upto 50 or can it be dynamic?

Best
Ankit

Hi @Ankit_Joshi,

Regarding the Field based model you described. Do I have to create static level upto 50 or can it be dynamic?

In MongoDB the schema is flexible so you can add fields dynamically as user profile grows.

The problem I have with lots of potentially large arrays as I expect in the chosen model. Considering that a user might end up with this design in an array of thousands of elements and nested elements which is bad for performance and scalability.

Additionally, you can endup reaching the 16mb document size limit this way.

In my model the actual large arrays are stored in seperate documents and the levels only maintain the pointer id.

Best
Pavel

1 Like

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