node.js - mongodb: how to populate a single nested subdocuments -
i writing node.js application using mongodb , mongoose. seems having issue in understanding how query "single nested subdocuments". if have example following:
var childschema = new schema({ name: 'string' }); var parentschema = new schema({ // array of subdocuments children: [childschema], // single nested subdocuments. caveat: single nested subdocs work // in mongoose >= 4.2.0 child: childschema });
from mongoose documentation, how populate child subdocument single nested scenario? tried use ".populate("child").exec..." child object returns null though can find if run db.child.find() mongo command line. saw in documentation, have call "parent.children.id(_id)" in case not know _id ahead of time.
update: removed populate , appears working now.
you storing child document embedded 1 inside parent document, there no need populate. populate cases storing reference other document want populated while running query. example such case :
var childschema = new schema({ name: 'string' }); var childmodel = mongoose.model("child", childschema); var parentschema = new schema({ // array of subdocuments children: [childschema], child: { type: schema.objectid, ref: 'child' } });
now if try populate child
while querying parent
, should work.
Comments
Post a Comment