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

2018-08-24 13:36:22 +08:00
 3rdFaust
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 的回调里面和外面是同一个意思么?

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

为什么要每次取一个 id 出来去查??? 一个查询就出来啦
3rdFaust
2018-08-24 16:18:55 +08:00
@zkx664832 我是新来的土包子,谢谢大哥指教。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/482851

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX