Populate mongoose array of objects Populate mongoose array of objects mongoose mongoose

Populate mongoose array of objects


It wont work as system is not a property of systems. You need to populate it like

Module.findOne({project: pId}).sort('-created')    .populate('systems.0.system').exec(function (err, doc){})Module.findOne({project: pId}).sort('-created')        .populate('systems.1.system').exec(function (err, doc){})

So you should have a for loop and iterate over it to populate all the documents. Else you should modify your model to make it work better.

var ModuleSchema = new Schema({systems: [{    system: {            type: Schema.ObjectId,            ref: 'System'        },            quantity: {            type: Number        }    }]});

Change your model to this and it will make easy for you.

 var ModuleSchema = new Schema({        systems: {           system: [{                type: Schema.ObjectId,                ref: 'System'           }],           quantity: {           type: Number        }        }    });