I'm creating an app with the merng stack that allows user to log a set they have done for an exercise to their account. I am currently working on mongoose schemas and have two schemas that are fairly similar, Workout
and Exercise
.
models/Workout.js
const { model, Schema } = require("mongoose");
const workoutSchema = new Schema({
workoutName: String,
username: String,
createdAt: Number,
exercises: [
{
exerciseName: String,
sets: [
{
weight: Number,
reps: Number,
createdAt: Number,
notes: String
}
]
}
],
notes: String
});
module.exports = model("Workout", workoutSchema);
models/Exercise.js
const { model, Schema } = require("mongoose");
const exerciseSchema = new Schema({
exerciseName: String,
username: String,
sets: [
{
weight: Number,
reps: Number,
createdAt: Number,
notes: String
}
]
});
module.exports = model("Exercise", exerciseSchema);
The workout document will only contain an array of exercises and the sets that were logged during a workout (each workout is limited to 4 hours). The exercise document will contain an array of all the sets a user has ever logged while using the app. The problem I am facing is that whenever a new workout document is created with an exercise they have already done before, it creates a new ID for the exercise instead of being the same as the one in the exercise document. I want the workout document to recognise that an ID already exists for an exercise if it has been logged previously.
After researching, I believe that I need to reference the exercise schema. However, even with the answers on SO, I'm still unsure on how to achieve this with my project.
question from:
https://stackoverflow.com/questions/65849390/referencing-another-schema-in-mongoose 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…