title: Mongodb Update Multiple array objects at once
body: <p>Im trying to enter every <code>family</code> Documents, and on each of those documents, 
  Check if theres a <code>user</code> in the <code>users array</code> (user is an <code>object</code>) that matching the results, and then update the spesific one.</p>
  
  <p>How would i accomplish so the query would go trough documents, and in every users, if matched criteria, update it?</p>
  
  <p><strong>First attempt code:</strong></p>
  
  <pre><code>            var From_joinedTime = new Date().getTime() - (60*60*24*2*1000);
  
     return Family.updateMany({"users.permission" : 0, "users.joined_date" : {$lte: From_joinedTime}},{$set: {"users.$.permission" : 1}},{multi:true}).then(function (response) {
                      return response;
                  });
  </code></pre>
  
  <p><strong>Please note:</strong></p>
  
  <p>i tried to Google, and came over the same issue, and tried the answer by <em>Neil Lunn</em> there: <a href="https://stackoverflow.com/questions/4669178/how-to-update-multiple-array-elements-in-mongodb">How to Update Multiple Array Elements in mongodb</a></p>
  
  <p>Code modified from <em>Neil Lunn</em>:</p>
  
  <pre><code>Family.update(
                  { "users.permission":0, "users.joined_date":  {$lte: From_joinedTime}},
                  { "$set": { "users.$[elem].permission": 1 } },
                  { "arrayFilters": [{ "users.permission": 0,
                          "users.joined_date":  {$lte: From_joinedTime} }], "multi": true }
              )
  </code></pre>
  
  <p>But that failed.</p>
  
  <p>i found this question that had same issue, but i could not find the answer good enough: <a href="https://stackoverflow.com/questions/54843259/cannot-read-property-castforquery-of-undefined-at-castarrayfilters-in-node-js">Cannot read property &#39;castForQuery&#39; of undefined at castArrayFilters in Node.js</a></p>
  
  <p>Error:</p>
  
  <pre><code>(node:9740) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'castForQuery' of undefined
  at castArrayFilters (\node_modules\mongoose\lib\helpers\update\castArrayFilters.js:60:37)
  at _castArrayFilters (\node_modules\mongoose\lib\query.js:1736:5)
  at model.Query._updateThunk (\node_modules\mongoose\lib\query.js:3449:3)
  at model.Query.&lt;anonymous&gt; (\node_modules\mongoose\lib\query.js:3535:23)
  at model.Query._wrappedThunk [as _execUpdate] (\node_modules\mongoose\lib\helpers\query\wrapThunk.js:16:8)
  at process.nextTick (\node_modules\kareem\index.js:369:33)
  at process._tickCallback (internal/process/next_tick.js:61:11)
  </code></pre>
  
  <p><strong>What do i do?</strong></p>