Skip to content
js
const HomeWorkSchema = mongoose.Schema({
  title: String,
  content: String,
  // 作业和学生产生关联
  studentId: {
    ref: 'Student',
    type: mongoose.SchemaTypes.ObjectId
  }
}, {
  timestamps: {
    createAt: 'createTime',
    updateAt: 'updateTime'
  }
})

;(async () => {
  // 关联查询,通过作业找到学生并显示学生的信息
  const res = await HomeWork
    .findById('6301d1bda05f4b3b5339a883')
    .populate('studentId', { name: 1 }) // 只显示 name
})()