V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
3rdFaust
V2EX  ›  Node.js

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

  •  1
     
  •   3rdFaust · Aug 24, 2018 · 5595 views
    This topic created in 2814 days ago, the information mentioned may be changed or developed.
    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 replies    2018-08-24 16:18:55 +08:00
    wode234
        1
    wode234  
       Aug 24, 2018   ❤️ 1
    如果你问 usersIds 和 userProfiles 在回调里外是不是同一个意思,那肯定是同一个意思的,我看你是想吧 usersIds 中所有的用户信息 userData 存到 userProfiles 中,应该是 ok 的
    3rdFaust
        2
    3rdFaust  
    OP
       Aug 24, 2018
    @wode234 因为在 java 里面同样情况会有一个下划线的标记,我不知道在 js 里是什么样的限制。
    zkx664832
        3
    zkx664832  
       Aug 24, 2018   ❤️ 1
    find({
    tag: "DATA", uId: { $in: usersIds }
    })

    为什么要每次取一个 id 出来去查??? 一个查询就出来啦
    3rdFaust
        4
    3rdFaust  
    OP
       Aug 24, 2018
    @zkx664832 我是新来的土包子,谢谢大哥指教。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5900 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 03:14 · PVG 11:14 · LAX 20:14 · JFK 23:14
    ♥ Do have faith in what you're doing.