猫鼬填充达不到3个级别

时间:2020-05-28 11:36:15

标签: node.js mongodb mongoose

我正在尝试填充嵌套在模型中但未填充的字段。

这是我模型中的一个字段。

pendingChanges: {
credentials: {
  university: {
    name: { type: String },
    major: { type: String },
    majorGpa: { type: Number },
  },
  school: {
    name: { type: String },
    degreeType: { type: String },
    degree: { type: String },
  },
  subjects: [{ type: mongoose.Schema.Types.ObjectId, ref: 'subject' }],
  workExperience: {
    type: { type: String },
    from: { type: Date },
    to: { type: Date },
  },
 },
},

我正在尝试填充嵌套在其中的主题键。

这是我到目前为止所做的。

const teacher = (await this.findById(id))
    .populate({
      path: 'pendingChanges',
      populate: {
        path: 'credentials',
        populate: {
          path: 'subjects',
        },
      },
    });

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。这就是我所做的,以使其起作用。

const data = await this.findOne(query)
    .populate({
      path: 'pendingChanges.credentials.subjects',
    });