title: MongoDB: Replace object in array
body: <p>I am trying to replace/update a whole object in an array to it's latest values, but I cannot get it to work.</p>
  <p>Db looks like this: (Note: there is only 1 main object in this collection)</p>
  <pre><code>    {
          &quot;_id&quot;: {...},
          &quot;something that doesnt matter&quot;: {...},
          &quot;var1&quot;: {
              &quot;var2&quot;: [{...}, {...}, {...}, {...}, {...}],
              &quot;var3&quot;: [{...}, {...}, {...}, {...}, {...}]
          },
          &quot;something that doesnt matter&quot;: {...}
      }
  </code></pre>
  <p>I need to update a certain object from array var2, I have the object ID or there is a custom ID in the object that I can also get it with (id == updatedObject.id)</p>
  <p>This worked but I cannot get it to work with a custom array id</p>
  <pre><code>await db.collection(&quot;collectionName&quot;).findOneAndUpdate(
      {&quot;var1.var2&quot;: { $exists: true }},
      { $set: { &quot;var1.var2.1&quot;: updatedObject } }
  );
  </code></pre>
  <p>I have the ID of the object already in the array on the db, but idk how to update it from var1.var2.<strong>ID</strong>,</p>
  <p>so basically what I need is <code>{ $set: { &quot;var1.var2.**ID**&quot;: updatedObject } }</code> but I cant seem to find out how to get it to work.</p>
  <p>Cause I dont want to update the whole array, and I also dont want to update a single variable in the object. I need to update the whole object.</p>
  <p>Thank you in advance for your replies.</p>