How to populate through array of objects in mongoose

非 Y 不嫁゛ 提交于 2019-12-12 03:54:15

问题


I have a data like follows

 "student" : [ 
ObjectId("58500ea5ef914125073b040f"),
ObjectId("58500ea5ef914125073b042e")

],

my model,

  student: [{type: Schema.ObjectId,ref: 'Student'}],

I want to populate student in these array,

  Classroom.findById(id).populate('student.student'){}

It is not working,can anyone please suggest help.Thanks.


回答1:


From what i can see in your data, student is the array of students, so you need to just write student in the populate query.

this should work for you :

Classroom.findById(id).populate('student').exec(function(err,result){
    ...
});


来源:https://stackoverflow.com/questions/41135110/how-to-populate-through-array-of-objects-in-mongoose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!