Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

node.js - Referencing another schema in mongoose

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can reference by _id and don't need to store entire object exercise into workout collection.

And after that you can use populate defined as:

Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s).

So, your workout schema should be:

const workoutSchema = new Schema({
    workoutName: String,
    username: String,
    createdAt: Number,
    exercises: [
        {
          type: mongoose.Schema.Types.ObjectId,
          ref: 'Exercise',
        }
    ],
    notes: String
});

With this schema you can use populate() or $lookup to do a "SQL Join" to get all values from exercise into workout where _id are equals. You can use other field to compare too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...