V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
3rdFaust
V2EX  ›  Node.js

问个根据 MongoDB 查询结果递归的问题

  •  1
     
  •   3rdFaust · 2018-08-24 13:36:22 +08:00 · 4449 次点击
    这是一个创建于 2043 天前的主题,其中的信息可能已经有所发展或是发生改变。
    function doIteration(usersIds,userProfiles) {
        if(userProfiles==null)
            userProfiles=[];
        let userId=usersIds.shift();
        app.db.db().collection("main").findOne({tag: "DATA", uId: userId}, (err, userData) => {
            if(userData!=null)
                userProfiles.push(userData);
            if(usersIds.length!==0)
                doIteration(usersIds,userProfiles);
            else
                proceed(userProfiles);
        });
    }
    

    这么迭代可以么?特别的, usersIds 和 userProfiles 这两个变量,在 findOne 的回调里面和外面是同一个意思么?

    4 条回复    2018-08-24 16:18:55 +08:00
    wode234
        1
    wode234  
       2018-08-24 15:40:20 +08:00   ❤️ 1
    如果你问 usersIds 和 userProfiles 在回调里外是不是同一个意思,那肯定是同一个意思的,我看你是想吧 usersIds 中所有的用户信息 userData 存到 userProfiles 中,应该是 ok 的
    3rdFaust
        2
    3rdFaust  
    OP
       2018-08-24 15:47:32 +08:00
    @wode234 因为在 java 里面同样情况会有一个下划线的标记,我不知道在 js 里是什么样的限制。
    zkx664832
        3
    zkx664832  
       2018-08-24 15:58:36 +08:00   ❤️ 1
    find({
    tag: "DATA", uId: { $in: usersIds }
    })

    为什么要每次取一个 id 出来去查??? 一个查询就出来啦
    3rdFaust
        4
    3rdFaust  
    OP
       2018-08-24 16:18:55 +08:00
    @zkx664832 我是新来的土包子,谢谢大哥指教。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3352 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:41 · PVG 21:41 · LAX 06:41 · JFK 09:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.