简体   繁体   English

使用 mongo db 查询用户的推荐

[英]Query referrals of a user with mongo db

I want to create a referral system with nodejs and mongoose, and I need help with the mongodb queries我想用 nodejs 和 mongoose 创建一个推荐系统,我需要 mongodb 查询方面的帮助

the user object:用户 object:

    {

    email: String,
    referredBy: ObjectId,
    points: Int,
    _id: ObjectId
    
    }

First query: each time that a user wins points I want to add points to his main referral, and then to that user referral and the same with that user, this system has 3 level of referrals第一个查询:每次有用户获得积分我想给他的主推荐加积分,然后给那个用户推荐和那个用户一样,这个系统有3级推荐

Second query: get all referred users that a user referred to (this would be easy it was just one level (doing第二个查询:获取用户引用的所有推荐用户(这很容易,它只是一个级别(做

this.user.find({referredBy: userId}))

) )

but I need to get all 3 levels, first all user that user referred to and the same with those users and the same with these users但我需要获得所有 3 个级别,首先是用户引用的所有用户,并且与这些用户相同,并且与这些用户相同

also with this second query would it be possible to add some field to each level?也可以通过第二个查询向每个级别添加一些字段吗?

like all referred users of first level to have a 'level': 'first' so i could later distinguish them像所有第一级的推荐用户一样有一个 'level': 'first' 所以我以后可以区分它们

First question:第一个问题:

Process each user in a loop in your application.在您的应用程序中循环处理每个用户。 Retrieve parent user, update parent, retrieve parent's parent, etc.检索父级用户、更新父级、检索父级的父级等。

Second question:第二个问题:

Use $lookup and $unwind to join users onto themselves as many times as is necessary.根据需要,使用 $lookup 和 $unwind 将用户加入到他们自己身上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM