I am writing an application in which there are workouts consisting of exercises. I create such models:

const valuesOfExercise = new Schema({ exercise: {type: Schema.Types.ObjectId, ref: "Exercise"}, repeats: {type: Number, required: true}, measure: {type: Number, required: true}, order: {type: Number} }) const workoutSchema = new Schema({ user: { type: Schema.Types.ObjectId, ref: "User" }, date: {type: Date}, exercises: [valuesOfExercise] }) 

At the front there is an opportunity to remove an exercise from a workout or to rearrange the exercise.
Accordingly, the order of the exercises is the order value, which exercises are sorted by. After removing each exercise, a hole is formed that needs to be closed. From the front comes the object, which must be removed. The code looks like this:

 const {itemToDelete} = req.body Workout.find({user: req.payload._id}).populate({path: 'exercises.exercise'}) .exec((err, item)=>{ item.map((ex, i)=>{ let arr = [...ex.exercises] arr.splice(itemToDelete.order, 1) arr.map((ex, index)=>{ ex.order = index; }) console.log(arr) }) }) 

Nothing else has come up with. The array is new with the necessary values, with this everything is ok. But how to change the order values ​​in the database - I have no idea, actually this is the question. I will welcome any useful comments, since I am a beginner in back-up and, in fact, this is my first more or less serious experience with mongoose.

    0