title: How to update date string in array to date format in mongoDB?
body: <p>My mongoDB collection looks like this:</p>
  <pre><code>[
    {
      &quot;id&quot;: &quot;myid&quot;,
      &quot;field&quot;: {
        &quot;total&quot;: 1,
        &quot;subfield&quot;: [
          {
            &quot;time&quot;: &quot;2020-08-06T08:33:57.977+0530&quot;
      
          },
          {
            &quot;time&quot;: &quot;2020-05-08T04:13:27.977+0530&quot;
            
          }
        ]
      }
    },
    {
      &quot;id&quot;: &quot;myid2&quot;,
      &quot;field&quot;: {
        &quot;total&quot;: 1,
        &quot;subfield&quot;: [
          {
            &quot;time&quot;: &quot;2020-07-31T10:15:50.184+0530&quot;
          }
        ]
      }
    }
  ]
  </code></pre>
  <p>I need to update all the documents and convert date string in the field <code>time</code> available in the <code>subfield</code>array to mongoDB ISO date format.</p>
  <p>I have thousands of documents and hundreds of objects in <code>subfield</code> array</p>
  <p>I'm aware of the aggregate function <code>$todate</code> and <code>$convert</code>.
  But I don't want to use aggregation because,</p>
  <ol>
  <li><p>To use <code>$todate</code> or <code>$convert</code>, I need to unwind the <code>field.subfield </code> array which is again an expensive operation.</p>
  </li>
  <li><p>I want to update my document and save it with the date format.</p>
  </li>
  </ol>
  <blockquote>
  <p>My MongoDB server version: 4.0.3</p>
  </blockquote>
  <p>I tried the following but it doesn't seem to work and also doesn't return any errors.</p>
  <pre><code>db.collection.find().forEach(function(doc) { 
  doc.field.subfield.time=new ISODate(doc.field.subfield.time);
  db.collection.save(doc); 
  })
  </code></pre>