Mongoose填充多个嵌套文档

16

我已经四处搜寻,但无法想出如何构建以下的填充查询,首先这是我的模型:

const CourseSchema = new Schema({
    classes: [{ type: Schema.Types.ObjectId, ref: 'Classroom' }]
});

const ClassSchema = new Schema({
    location: { type: mongoose.Schema.Types.ObjectId, ref: 'Location' },
    instructors: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
});

我有一个终端点,可以获取单个课程,但是我想要填充classes字段以及classes中的locationinstructors字段。目前,我只能填充classes中的instructors字段或location字段,但不能同时填充它们。现在,以下是我的代码:

    Course
        .findById(req.params.courseId)
        .populate({
            path: 'classes',
            populate: {
                path: 'instructors',
                model: 'User'
            }
        })
我还能如何填充classes字段中的location? 谢谢。

请使用此插件 https://www.npmjs.com/package/mongoose-deep-populate - styopdev
我想知道这是否有效:.populate({ path : 'classes', populate : { path : 'instructors location' } }) - robertklep
@zangw 真遗憾 :-( 我本来希望在这种情况下多个人口选项(以空格分隔的字段名称)也能起作用。 - robertklep
@robertklep,是的,我之前也是这么想的。然而,它并不起作用... - zangw
@zangw 谢谢你的检查!=D - robertklep
显示剩余3条评论
3个回答

76

一个有趣的替代方法是将数组传递给嵌套的populate。

Course
    .findById(req.params.courseId)
    .populate({
        path: 'classes',
        model: 'Classroom',
        populate: [{
            path: 'instructors',
            model: 'User'
        },
        {
            path: 'location',
            model: 'Location'
        }]
    })

2
如果在这个填充的数组中需要再添加一个填充,是否有可能呢? - Honchar Denys
太棒了!像魔法一样运作!这正是我所需要的。谢谢。 - John
甜蜜的回答。谢谢。 - Alex Onozor
我看到了答案,觉得它很棒。我本来想点赞的,但是发现我已经点过赞了。我们人类真的很健忘:) - yalcinozer

13

在 mongoose v4 下,请尝试这个,这是一个关于Population的好链接。

   Course
        .findById(req.params.courseId)
        .populate({
            path: 'classes',
            model: 'Classroom',
            populate: {
                path: 'instructors',
                model: 'User'           
            }
        })
        .exec(function(err, cour) {
            if (err)
                console.log(err);
            else {
                Course.populate(cour, 
                    {
                        path: 'classes.location',
                        model: 'Location',
                    }, function(err, c1) {
                        if (err)
                            console.log(err);
                        else
                            console.log(util.inspect(c1, { showHidden: true, depth: null }));                       
                    })
            }
        })

使用deep-populate插件是有效的,但我尝试了你提供的答案,它只填充了instructors字段,location字段只返回id。 - Alistair

5
Try the below code    


getAllByQuery: function (query, callback) {
    this
      .find(query, {capId: 1})
      .populate({path: 'capId',
        model: 'cap',
        select: {
          'capDetail': 0,
          'area': 0,
        },
        populate: [{
          path: 'ad_Id',
          model: 'Ad',
          select: { 'Level': 1}
        },
        {
          path: 'ctgs',
          model: 'Ctgry',
          select: { '_id': 0}
        }]
      })
      .exec(callback);
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接