title: How to change a single value inside of an object inside of an array in a mongoose model and save it?
body: <p>I am making a website where you can sign in, and create tasks, and then check them out. I am working with mongoose, and the tasks are saved inside an array of type Task in a User model. Each task has a "done" value which is either "true" or "false"(not a boolean), when the user checks a task as done an ajax Put request is sent to the server and needs to change the "done" value. I cant change the done value. I think the problem is accessing the "done" in the "task" object in the Task array in the User model.</p>
  
  <p>I've tried all sort of syntaxes shown in the mongoose official website, but nothing is seems to work. </p>
  
  <pre><code>//send a task(a string with the actual task) and the new done situation to the server
      function putDone(task,done){
          $.ajax({
              type: 'Put',
              data:{
                  task,
                  done,
              },
              url: '/tasks/tasks',
          })
      }
  </code></pre>
  
  <pre><code>//find the task from all the user's tasks and replaces the done situation
  router.put("/tasks",(req,res) =&gt; {
      for(var i = 0; i &lt; req.user.tasks.length; i++){
          if(req.user.tasks[i].task == req.body.task){
              User.findOneAndUpdate(
                  {'username': req.user.username},
                  {'$set': { 'req.user.tasks[i-1].done': req.body.done }},
                  {'new': true},
                  function(err,updatedUser){
                      if(err){
                          console.log(err);
                      }else{
                          console.log(req.body.task + ", " + req.body.done);
                          console.log(updatedUser.tasks[i-1]);
                      }
                  }
              );
          }
      }
  });
  </code></pre>
  
  <p>the models as asked in the answers:</p>
  
  <pre><code>
  //task schema
  //var Task = mongoose.Schema.Types.Task;
  let userSchema = mongoose.Schema({
      email:{
          type: String,
          required:true,
      },
      username:{
          type: String,
          required: true,
      },
      password:{
          type: String,
          required: true,
          minLength: 8,
          maxLength: 20,
      },
      archive:{
          type: Array,
          required: false,
      },
      tasks:{
          type: Array,
          required: false,
      }
  });
  
  let User = module.exports = mongoose.model('User', userSchema);
  </code></pre>
  
  <pre><code>
  //task schema
  
  let taskSchema = mongoose.Schema({
      task:{
          type: String,
          required: true,
          minlength: 2,
      },
      done:{
          type: Boolean,
          default: false,
          required: true,
      }
  });
  
  let Task = module.exports = mongoose.model('Task', taskSchema);//find by first param the collection, can add third param of actual collection name
  </code></pre>