Joe Joe - 1 year ago 77
Node.js Question

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
and for each of the elements the field
has also been populated.

I have tried a few things along the lines of:


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

Thanks in advance!

Answer Source

hope this helps.

        path: 'elements',
        populate: { 
             path:  'components', 
             model: 'Component'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download