Mongoose: multiple query populate in a single call

后端 未结 4 490
盖世英雄少女心
盖世英雄少女心 2020-12-01 03:08

In Mongoose, I can use a query populate to populate additional fields after a query. I can also populate multiple paths, such as

Person.find({})
 .populate(\         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 03:55

    This is how it's done based on the Mongoose JS documentation http://mongoosejs.com/docs/populate.html

    Let's say you have a BookCollection schema which contains users and books In order to perform a query and get all the BookCollections with its related users and books you would do this

    models.BookCollection
        .find({})
        .populate('user')
        .populate('books')
        .lean()
        .exec(function (err, bookcollection) {
        if (err) return console.error(err);
        try {
            mongoose.connection.close();
            res.render('viewbookcollection', { content: bookcollection});
    
        } catch (e) {
            console.log("errror getting bookcollection"+e);
        }
    

提交回复
热议问题