我得到了以下文件:
{
"_id": "5b5df332879b4769739e3ed0",
"name": "Back workout",
"exercises": [{
"_id": "5b5df9a7879b4769739e3ed1",
"results": [{"reps":"10","weight":"85"}, {"reps":"8","weight":"70"}]
}]
}
我想基于_id
填充练习,并附加results
数据。
预期输出:
{
"_id": "5b5df332879b4769739e3ed0",
"name": "Back workout",
"exercises": [{
"_id": "5b5df9a7879b4769739e3ed1",
"name": "EXERCISE_NAME",
"results": [{"reps":"10","weight":"85"}, {"reps":"8","weight":"70"}]
}]
}
我的模式如下:
const WorkoutSchema = new Schema({
_id: Schema.Types.ObjectId,
name: String,
exercises: [{
_id: { type: Schema.Types.ObjectId, ref: 'Exercise' },
results: Array,
}],
createdAt: Date,
updatedAt: Date,
});
我的查询如下:
exports.listAll = async (request, h) => {
const workoutCollection = await Workout
.find()
.populate('exercise');
return { data: workoutCollection };
};