I have the following model where I store a reference to a another document inside an array of another model:
const ProgramExecutionSchema = new Schema({
user: { type: Schema.Types.ObjectId, ref: "User", required: true },
executions: [
{
programId: {
type: Schema.Types.ObjectId,
ref: "Program.program",
required: true,
},
date: { type: Date, required: true },
executionRate: { type: Number, required: true, min: 0, max: 100 },
grade: { type: Number, required: true, default: 0, min: 0, max: 10 },
},
],
});
and this is the model it refers to (I want to populate a specific program inside the program array) :
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" },
},
],
});
When trying to populate the 'programId' field it just ignores it. But the _id that I store inside 'ProgramExecution' model is correct. So I am not sure if I am using the right query for population. here is what I tried:
const programExecutions = await ProgramExecution.findOne({
user: userId,
}).populate({
path: 'executions',
populate: {
path: 'programId',
model: 'Program',
},
});