How to select two table (document) value at a time by user id in mongoose?

后端 未结 3 1363
抹茶落季
抹茶落季 2021-01-21 16:05

I am using NodeJs, ExpressJs with Mongodb and Mongoose. I want to select two document\'s value by user id.

I have three model User, Academic and Career. I have made a re

3条回答
  •  清酒与你
    2021-01-21 16:53

    This is the simplest query we can make for find user with given query Id

    router.get('/getInfoById/:id', async (req, res) => {
         const user = await User.aggregate([
               { $match: { _id: mongoose.Types.ObjectId(req.query.id) }},
               { $lookup: {
                  from: "career",
                  localField: "_id",
                  foreignField: "user",
                  as: "careers"
               }},
               { $lookup: {
                  from: "academic",
                  localField: "_id",
                  foreignField: "user",
                  as: "academics"
               }}
            ])
        res.send(user);
    });
    

提交回复
热议问题