Mongoose nested query on Model by field of its referenced model

前端 未结 3 689
旧时难觅i
旧时难觅i 2020-11-29 02:22

It seems like there is a lot of Q/A\'s on this topic on stackoverflow, but I can\'t seem to find an exact answer anywhere.

What I have:

I

3条回答
  •  失恋的感觉
    2020-11-29 02:42

    In case anyone comes across this in more recent times, Mongoose now supports join like functionality with a feature called Populate.

    From the Mongoose documentation:

    Story.findOne({ 
        title: 'Casino Royale' 
    }).populate('author').exec(function (err, story) {
        if (err) return handleError(err);
        console.log('The author is %s', story.author.name);
        // prints "The author is Ian Fleming"
    });
    

    http://mongoosejs.com/docs/populate.html

提交回复
热议问题