title: How to update a an object field, inside an array that is inside an array
body: <p>I want to update a <code>note</code> inside an array that is inside an array having the name of my list.</p>
  
  <p>I have the <code>_id</code> of my user, <code>name: 'Test'</code> of the list, the <code>_id</code> of the book and the <code>note</code> to update to.</p>
  
  <p>My model is as follow :</p>
  
  <pre class="lang-js prettyprint-override"><code>const userSchema = new Schema({
      _id,
      ...,
      lists: [
          {
              name: String,
              books: [
                  {
                      _id: { type: Schema.Types.ObjectId, ref: 'Books' },
                      note: { type : Number, default: null }
                  }
              ]
          }
      ]
  })
  </code></pre>
  
  <p>I tried with <code>$set</code> and <code>$push</code> but got instead an another list with a name 'Test'. I tried as well to search the user _id and manually find in javascript with .filter() to find my books, but there seem to be a solution with Mongoose.</p>
  
  <p>Tried to do the follow query, but without success</p>
  
  <pre class="lang-js prettyprint-override"><code>({_id: req.myUser._id, 'lists': { $elemMatch: { 'name' : 'General' }}})
  </code></pre>
  
  <p>Example, I want to update <code>qozirjqzàçorizq</code> to 15 :</p>
  
  <pre><code>{
      _id: qoizrhqzoi, &lt;--------------- I know this
      lists: [
          {
              name:'General', &lt;--------------- I know this
              books: [
                  {
                      _id: qozirjqzàçorizq, &lt;---------- I know this
                      note: 12      &lt;-------------- Want to change that
                  }
              ]
          },
          {   
              name: 'Perso1'
              books: [
                  {
                      _id: 8qzrq654rz,
                      note: 1
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>My json comes in the following format but can be changed to only have the 4 fields _id, name, _id and note.</p>
  
  <pre><code>        {
              _id: qozirjqzàçorizq,
              note: 15
          }
  </code></pre>
  
  <p>How do I update this particular field (note) inside two nested arrays ?</p>
  
  <p>EDIT : I've managed to update the the book that is noted, but it does not upsert, it always replace the current one, it's not acting as an array</p>
  
  <pre class="lang-js prettyprint-override"><code>User.findOneAndUpdate(
          {   _id: req.myUser._id,
              "lists" : { $elemMatch : 
                  { name: req.params.listName }
              } 
          },
          {
              $set: { 'lists.$.books': req.body}
          },
          {
              new: true,
              upsert: true
          })
      .exec()
      .then(res =&gt; {
          console.log(res)
      })
      .catch( err =&gt; {
          console.error(err)
      }
  </code></pre>