开发者

suggestions for creating referral based system in mongodb?

i'm looking to create a simple referral based system in mongodb, where a user can user a code to invite other users. all users will be part of the same code/group. users of that group can also invite other people to that group OR start their own. in this case, that user becomes a part of TWO groups (two different codes).

i want to keep track of the following things:

1) for a given code, how many people did you refer 2) which codes is a user a part of (can be a part of many codes/groups)

the simplest collection to contain all the info i need seems to be something like:

string Code string UserInitiated string UserReceived

so with those three fields in a collection, i can always tell how many times a user initiated a code (referred someone). who they referred. which code/group a particular user is a part of.

does that see开发者_如何学JAVAm pretty much the easiest way to do it?


I don't quite see how your suggested schema will give you the information you need in an efficient way.

I'd suggest the following :

db.groups {
    _id: <group code>,
    creator: <UUID of creator>,
}

db.members {
    _id: <UUID of group member>,
    groupCode: <group code>
    referrals: [
        <UUID of referred user>
    ]
    referralCount: <size of referrals array for speed/convenience>
}

CREATE GROUP :
db.groups.save({_id: <group code>, creator:<UUID of creator>})
db.members.save({_id:<UUID of creator>, groupCode: <group code>})

REFER USER :
db.members.update({_id: <UUID of referrer>}, {$push:{referrals: <UUID of referred user}, $inc:{referralCount:1}})

FOR A GIVEN CODE, HOW MANY PEOPLE DID YOU REFER :
db.members.find({_id: <UUID of user>, groupCode: <group code>}) -> first result .referralCount

WHICH CODES IS A USER PART OF :
db.members.find({_id: <UUID of user>}) -> results .groupCode

OR

db.members.distinct("groupCode", {_id: <UUID of user>}) for a direct array of codes

The reason I'd split it up is because maintaining the member list inside the groups document as an embedded array will complicate certain updates, may run into the 16mb document limit and will drastically increase bandwidth requirements for your queries (note that you'd have to find the entire group document for each member query in such cases).

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜