繁体   English   中英

填充嵌套在 Mongoose 对象数组中的文档不起作用

[英]populating a document nested in array of objects in Mongoose don't work

我正在尝试填充嵌套在对象数组中的文档,但似乎没有任何效果。

我想填充此程序文档中的锻炼字段。

这是程序文件:

const ProgramSchema = new Schema({
  user: { type: Schema.Types.ObjectId, ref: "User", required: true },

  program: [
    {
      day: {
        type: String,
        required: true,
        enum: [
          "Sunday",
          "Monday",
          "Tuesday",
          "Wednesday",
          "Thursday",
          "Friday",
          "Saturday",
        ],
      },

      restDay: { type: Boolean },

      workout: { type: Schema.Types.ObjectId, ref: "Workout" },
    },
  ],
});

const Program = mongoose.model("Program", ProgramSchema);

这是锻炼文件:

const WorkoutSchema = new Schema({
  user: { type: Schema.Types.ObjectId, ref: 'User', required: true },

  trainingDayName: {
    type: String,
    enum: ['A', 'B', 'C', 'D', 'FB', 'aerobic'],
    required: true,
  },

  name: { type: String, required: true },

  description: { type: String },

  exercises: [
    {
      muscles: [{ type: String }],
      name: { type: String, required: true },
      description: { type: String },
      sets: { type: Number, required: true, min: 1, max: 7 },
      reps: { type: Number, required: true, min: 1, max: 50 },
    },
  ],

  time: { type: Number, min: 10, max: 300 },
});

const Workout = mongoose.model('Workout', WorkoutSchema);

原来解决方案是以下语法:

const program = (await Program.findOne({ user: userId })
.populate(
      'program.workout'
    )) as ProgramType;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM