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

后端 未结 3 1369
抹茶落季
抹茶落季 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:45

    For what I've read in the docs, I think what you are looking for is something like so:

    const career = await Career.find({ user: user._id});
    const academics = await Academics.find({ user: user._id});
    

    Or if you want to execute both queries at the same time:

    const careerOperation = Career.find({ user: user._id});
    const academicsOperation = Academics.find({ user: user._id});
    
    const [
        career, 
        academics
    ] = await Promise.all([career.exec(), academics.exec()]);
    

    Hope to have helped!

提交回复
热议问题