简体   繁体   English

Mongoose填充子文档数组

[英]Mongoose populating array of subdocuments

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'
    }
})

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. 我想要执行一个深度填充,以便当我请求实验时,我得到一个具有elementsresources数组的对象,并且对于每个元素,还填充了字段component

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? 任何人都可以为这种类型的操作提供正确的语法吗?

Thanks in advance! 提前致谢!

hope this helps. 希望这可以帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM