Kris Gergov
Kris Gergov

Reputation: 55

Database modelling (mongoDB)

I am trying to model a database for a fitness app. Currently the 4 main entities are as follows:

Exercise User Workout UserWorkout
id id id id
name email name userId (fk)
body_part name description workoutId (fk)
category password level date
age exerciseIds (fk) time_taken

The app will have default workouts as well as default exercises.


I would like the user to be able to add their own custom workouts/exercises that only they can see (in addition to the default ones) but I'm not sure on how to best structure the data?


Upvotes: 0

Views: 118

Answers (1)

Rafael Freitas
Rafael Freitas

Reputation: 201

Kris, MongoDB is a schemaless database, what makes it really flexible when it comes down to data modelling. There are different ways of achieving what you described, the one I would recommend would be adding nested documents to the user document if they belong to it. You would have something like this:

User {
  firstName: ...,
  lastName: ...,
  age: ...,
  weight: ...,
  exercises: [
     // User's exercise objects
  ],
  workout: [
     // User's workout objects
  ],
}

This way you can easily have access to information related to the user and avoid using expensive operations like $lookup or querying the database multiple times.

To handle the default exercises/workouts you can have a property in the respective objects like isDefault: true.

Upvotes: 1

Related Questions