I could not find any response to my issue. Probably also a search issue, but would appreciate some help. I have the following model:
var ChildSchema = new Schema ({
'name': string
});
var ClassSchema = new Schema ({
'name': string,
'children': [ChildSchema]
});
I have only declared a model for class.
mongoose.model ('Class', ClassSchema);
and then I have another model:
var TeacherSchema = new Schema ({
'name': string,
'favorite': {type: ObjectId, ref: 'Class.children'} // a child from class children
'least': {type: ObjectId, ref: 'Class.children'} // same as above
});
With relevant model:
mongoose.model ('Teacher', TeacherSchema);
I am trying to retrieve a teacher by ID and get the children name populated:
Teacher.findbyId (teacherId).populate (favorite least)...
This does not seem to work. Is this modelling correct? Is this possible?
(I am aware that my example might not be politically correct, so please forgive me...)
EDIT This is a reflection of something in project. I am aware that the children can be modelled as seperate collection, but in my project, I prefer to keep them as embedded documents (for various reasons). Each child does get an _id when pushed into the children array and class is saved. The teacher needs to point to a single child in a class in "favorite" and in "least", i.e. to keep the _id of that child.