How to populate through array of objects in mongoo

2019-09-05 15:17发布

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条回答
Bombasti
2楼-- · 2019-09-05 15:47

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){
    ...
});
查看更多
登录 后发表回答