'mongoose replace the current values in an array with new one NOT adding new values to the existing values in the array

Currently I have this mongoose schema and model:

let mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/whatever', { useNewUrlParser: true, useUnifiedTopology : true });

var Schema = mongoose.Schema;

var categorySchema = new Schema({
    name: String,
    description: String,
    sortorder: Number,
    isactive : { type: Boolean, default: true } ,
    schedule : [] ,        
});


var Category = mongoose.model('Category', categorySchema);

module.exports = Category;

schedule array get days of the week and open/close hours for everyday of the week. When I try to update the schedule array, it adds to the previous values but I want to wipe those values and write new values in the array. This is how I use the model in my express route:

app.post('/categoryschedule', (req, res) =>{
    let data = req.body  
    let filter = {_id : data.id}
    let updated = {schedule : data.schedule  }
   Category.findByIdAndUpdate(filter, updated, {new : true}, function (error, results){
       if (error) {
        console.log("-----error ------- :", error)
     }

        res.json({"schedule update ": "category schedule updated", "resultsfromscheduleupdate" : results})    
    
   }) 
  
  })

Currently, if I enter a schedule like 1,2,3,4,5,6,7 into the array and then edit the schedule to be 8, 9, 10, 11, 12, 13, 14 I get 1 2 3 4 5 6 7 8 9 10 11 12 13 14 but I want the update to replace 1 2 3 4 5 6 7 with 8 through 14 so array after update would only include values of 8 through 14.

So every time I update the schedule, it adds the new values to whatever existed in the array before the update. I used $set and read the doc but unable to do it. It seems like once I write to the array, I can't erase the values already exited and write new ones.

Maybe I misunderstood the doc. To be clear, my question is how to replace the values already exist in the array schedule with new one - wipe the array empty every time I update the schedule - and add the new values to the array.

schedule : Array
0: {weekdayname: 'Monday', weekdaynumber: 1, openhours: '10'...}
1: {weekdayname: 'Tuesday', weekdaynumber: 2, openhours: '10', …}
2: {weekdayname: 'Wednesday', weekdaynumber: 3, openhours: '10', …}
3: {weekdayname: 'Thursday', weekdaynumber: 4, openhours: '10', …}
4: {weekdayname: 'Friday', weekdaynumber: 5, openhours: '10', …}
5: {weekdayname: 'Saturday', weekdaynumber: 6, openhours: '10' …}
6: {weekdayname: 'Sunday', weekdaynumber: 0, openhours: '10' …}

Anyone with experience in mongoose arrays or what type of datatype for store arrays and array of objects in mongoose?

If it is not possible, then how do I get to specific array element, let us say, I want to get to schedule[0].openhours

How to I update using model.findOneAndUpdate ({filter by id}, {scheudle[0].openhours : "updated value"}) it is not updating the targeted doc.

How to reference array elements in mongoose update fn?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source