猫鼬填充子文档数组 [英] Mongoose populating array of subdocuments

查看:86
本文介绍了猫鼬填充子文档数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果已经提出此要求,我的搜索没有出现相同的情况.我有两个类似以下的模式:

Apologies if this has already been asked, my searches did not turn up the same situation. I have two schemas something like the below:

var experimentSchema = new mongoose.Schema({

    name  : 'string'
    elements :  [{
        type : mongoose.Schema.ObjectId, 
        ref: 'Element'
    }],
    resources : [{
        type : mongoose.Schema.ObjectId,
        ref  : 'Resource'
    }],

})


var elementSchema = new mongoose.Schema({
    name : 'string',
    component : {
        type : mongoose.Schema.ObjectId,
        ref  : 'Component'
    }
})

我想进行大量填充,以便在我请求实验时得到一个包含elementsresources数组的对象,并且对于每个元素,字段component也已填充.

I want to perform a deep population so that when I request an experiment I get an object with an array of elements and resources and for each of the elements the field component has also been populated.

我已经尝试了以下几种方法:

I have tried a few things along the lines of:

Experiment.findOne(query).populate(['resources','elements','elements.component']).exec(...)

没有成功.谁能为这种类型的操作提供正确的语法?

without success. Can anyone provide the correct syntax for this type of operation?

提前谢谢!

推荐答案

希望有帮助.

models.User.findOne(query)
      .populate([{
        path: 'elements',
        populate: { 
             path:  'components', 
             model: 'Component'
            }
      },{
        path:'resources'
      }])
      .exec(...)

这篇关于猫鼬填充子文档数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆