【问题标题】:"Select from where in" translate to mongoose“从哪里选择”翻译成猫鼬
【发布时间】:2018-10-25 17:44:11
【问题描述】:

我想从

获取所有的_id

collection.posts

_idUser 位于/等于_idFollowing 的位置

collection.use

我的代码是:

app.get("/api_posts_by_friend/:id", function(req,res){
    Post.find()
      .where('_idUser')
      .in(User.find({"_id" : mongoose.Types.ObjectId(req.params.id)}, "_idFollowing").exec())
      .exec(function (err, records) {
       if (err){
            console.log("err:"+err);
        } else {
            console.log("req: "+records);
            res.json(records);
        }
      });
});

【问题讨论】:

  • 请将示例文档发布为纯文本/代码而不是屏幕截图,以便我们更轻松地复制。
  • 哦,对不起,我会在下一篇文章中解决这个问题,对于这个,我已经找到了解决方案,谢谢!

标签: node.js mongodb mongoose nosql


【解决方案1】:

我找到了解决方案!谢谢。

新代码: //从数据库中获取好友的所有帖子

app.get("/api_posts_by_friend/:id", function(req,res){
    db.open( function(err,mongoclient){
        mongoclient.collection('users', function(err,collection){
            collection.find(objectId(req.params.id)).toArray(function(err,results){
                if (err){
                    res.json(err);
                } else {
                    mongoclient.close();
                    var friends = results;
                    console.log("results="+results[0]._idFollowing);
                        db.open( function(err,mongoclient){
                        mongoclient.collection('postagem', function(err,collection){
                            collection.find({_idUser: {$in: friends[0]._idFollowing}}).toArray(function(err,results){
                                if (err){
                                    res.json(err);
                                } else {

                                    res.json(results);

                                }
                                mongoclient.close();
                            });
                        });
                    });
                }
            });
        });
    })

; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 2013-06-12
    • 2014-02-11
    相关资源
    最近更新 更多