title: How to update array inside object, inside array mongoDB
body: <p>So the document contains an array of objects, each object containing it's own array. So how would I go about updating one of the elements in the array that's inside the object which is inside another array. I've read some things with $. But I don't understand completely how to use it to call a position. I know the position of the element. But I can't just say $[] because the position is defined in a variable and not a string...</p>
  <p>I've tried doing a simple</p>
  <p><code>db.collection.findOne({...}, (err, data) =&gt; {...});</code></p>
  <p>and then changing the arrays in the objects in the array in there with a simple:</p>
  <p><code>data.arr[x].type[y] = z; data.save().catch(err =&gt; {console.log(err)});</code></p>
  <p>But it doesn't save the new values I set for for the element of the array.</p>
  <p>Sample structure after proposed solution from @Tom Slabbaert:</p>
  <pre><code>Data.findOne({
      userID: 'CMA'
  }, (err, doc) =&gt; {
      if(err) {console.log(err)}
      if(doc) {
          for(var i = 0; i&lt;CMA.stockMarket.length; i++) {
              if(CMA.stockMarket[i].name == data.userID) {
                  for(var z = 0; z&lt;CMA.stockMarket[i].userStock.length; z++) {
                      if(z == company) {
                          var updateAmount = CMA.stockMarket[i].userStock[z]+args[1]
                          var updateKey = `stockMarket.${i}.userStock.${z}`
                          Data.updateOne({userID: 'CMA'}, {'$set': {[updateKey]: updateAmount}})
                      }
                  }
              }
          }
      }
  });
  </code></pre>
  <p>-------------------------EDIT-------------------------<br />
  So I tried changing some things around in the data base to see if that would fix the problem I was having. I modified the updated code that was provided by @Tom Slabbaert. But nothing seems to work for some reason :/ Here's what I have so far, at this point I hope it's just a syntax error somewhere. Cause this is really frustrating at this point. Note that I'm still using the for loops here to find if the info exists. And if not, push that info into the database. This might only be temporary until I find a better way / if there is a better way.</p>
  <pre><code>for(var i = 0; i&lt;CMA.userStocks.length; i++) {
      if(CMA.userStocks[i].name == data.userID) {
          for(var z = 0; z&lt;CMA.userStocks[i].shares.length; z++) {
              //console.log(CMA.userStocks[i].shares[z].companyName)
              if(CMA.userStocks[i].shares[z].companyName == args[0]) {
                  var updateKey = `CMA.userStocks.$[elem1].shares.$[elem2].amount`
  
                  Data.updateOne(
                      {userID: 'CMA'},
                      {
                          &quot;$inc&quot;: {
                              [updateKey]: args[1]
                          }
                      },
                      {
                          arrayFilters: [
                              {
                                  &quot;elem1.name&quot;: data.userID,
                                  &quot;elem2.companyName&quot;: args[0]
                              }
                          ]
                      }
                  )
                  purchaseComplete(); return;
              }
          }
          CMA.userStocks[i].shares.push({companyName: args[0], amount: parseInt(args[1])})
          CMA.save().catch(err =&gt; {console.log(err)});
          purchaseComplete(); return;
      }
  }
  CMA.userStocks.push({name: data.userID, shares: [{companyName: args[0], amount: parseInt(args[1])}]});
  CMA.save().catch(err =&gt; {console.log(err)});
  purchaseComplete(); return;
  </code></pre>
  <p>The data I'm trying to find and change is structured like the following:<br />
  And what I'm trying to change in the end is the 'amount' (which is an integer)</p>
  <pre><code>_id: (Not relavent in this question)
  userID: 'CMA'
  stockMarket: [...] (Not relavent in this question)
  userStocks: [
     Object: (position 0 in userStocks array)
        name: 'string' (equal to data.userID in the code)
        shares: [
            Object: (position 0 in shares array)
                companyName: 'string' (this is args[0] in the code)
                amount: integer
        ]
  ]
  </code></pre>