"Выберите откуда" перевести на мангуста - PullRequest
0 голосов
/ 25 октября 2018

Я хочу получить все _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);
        }
      });
});

1 Ответ

0 голосов
/ 25 октября 2018

Я нашел решение!Спасибо.

Новый код: // получать все сообщения от друзей из db

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();
                            });
                        });
                    });
                }
            });
        });
    })

;}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...