【问题标题】:How to populate through array of objects in mongoose如何通过猫鼬中的对象数组填充
【发布时间】:2017-04-29 08:11:09
【问题描述】:

我有如下数据

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

],

我的模特,

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

我想在这些数组中填充学生,

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

它不起作用,任何人都可以建议帮助。谢谢。

【问题讨论】:

  • 您是否将 ref 添加到模型的架构 student:[{ type: Schema.Types.ObjectId, ref: 'Student' }]
  • 是的,我做到了.....
  • 您能否展示您的架构代码,并确保您有适当的数据填充到数据库中。
  • 请发布您的课堂架构。
  • 您好发布了我的架构。

标签: node.js mongodb mongoose mongoose-populate


【解决方案1】:

从我在您的数据中可以看到,student 是学生的array,所以您只需在populate 查询中写上student

这应该适合你:

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

【讨论】:

    猜你喜欢
    • 2014-11-25
    • 2023-03-16
    • 1970-01-01
    • 2021-08-16
    • 2020-10-13
    • 2019-11-27
    • 1970-01-01
    • 2014-07-24
    相关资源
    最近更新 更多