javascript - How to populate an array of sub `ref`ed documents mongoose? -
i'm working on mean stack app mongoose version 4.4.0
, having problem populating array of sub-documents. (couldn't find solutions elsewhere on so.)
the following illustrates structure of models:
visa model
var visaschema = new mongoose.schema({ from: {type: schema.types.objectid, ref: 'country'}, to: {type: schema.types.objectid, ref: 'country'}, requirements: [ {type: schema.types.objectid, ref: 'requirement'} ] }); exports.model = mongoose.model('visa', visaschema);
requirement model
var requirementschema = new mongoose.schema({ visa: {type: schema.types.objectid, ref: "visa"}, title: string, body: string }); exports.model = mongoose.model('requirement', requirementschema);
the above structures one-to-many relationship between visa
, requirement
.
problem
i not able populate requirements
field in fact array of requirement
ref
objects.
this how i'm trying achieve it:
visa.find() //.populate('from', null, {shortname: from}) //.populate('to', null, {shortname: to}) .populate('requirements') .exec(function(err, result){ res.json(result); });
whenever try populate requirements
field, returns empty array on requirements
key.
however without population, returns array of objectids
:
[ { "_id":"56b329878848eba00c0f5535", "from"{ "_id":"56b2db1b57f9e71c1bfbadb9", "name":"germany", "shortname":"de", "__v":0 }, "to":{ "_id":"56b2db0f57f9e71c1bfbadb8", "name":"afghanistan", "shortname":"afg", "__v":0 }, "__v":3, "requirements": [ "56b331d764585e8c12c14dd1", "56b336431ffc49bc19bf1c96", "56b33675443f033c14e26d03" ] } ]
what doing wrong?
any kind of appreciated.
Comments
Post a Comment