title: MongoDB convert string type to float type
body: <p>Following the suggestions over here <a href="https://stackoverflow.com/questions/4973095/mongodb-how-to-change-the-type-of-a-field">MongoDB: How to change the type of a field?</a> I tried to update my collection to change the type of field and its value.</p>
  
  <p><strong>Here is the update query</strong> </p>
  
  <pre><code>db.MyCollection.find({"ProjectID" : 44, "Cost": {$exists: true}}).forEach(function(doc){
      if(doc.Cost.length &gt; 0){
          var newCost = doc.Cost.replace(/,/g, '').replace(/\$/g, '');
          doc.Cost =  parseFloat(newCost).toFixed(2);  
          db.MyCollection.save(doc);
          } // End of If Condition
      }) // End of foreach
  </code></pre>
  
  <p>upon completion of the above query, when I run the following command</p>
  
  <pre><code>db.MyCollection.find({"ProjectID" : 44},{Cost:1})
  </code></pre>
  
  <p>I still have <code>Cost</code> field as string.</p>
  
  <pre><code>{
      "_id" : ObjectId("576919b66bab3bfcb9ff0915"),
      "Cost" : "11531.23"
  }
  
  /* 7 */
  {
      "_id" : ObjectId("576919b66bab3bfcb9ff0916"),
      "Cost" : "13900.64"
  }
  
  /* 8 */
  {
      "_id" : ObjectId("576919b66bab3bfcb9ff0917"),
      "Cost" : "15000.86"
  }
  </code></pre>
  
  <p>What am I doing wrong here?</p>
  
  <p>Here is the sample document</p>
  
  <pre><code>/* 2 */
  {
      "_id" : ObjectId("576919b66bab3bfcb9ff0911"),
      "Cost" : "$7,100.00"
  }
  
  /* 3 */
  {
      "_id" : ObjectId("576919b66bab3bfcb9ff0912"),
      "Cost" : "$14,500.00"
  }
  
  /* 4 */
  {
      "_id" : ObjectId("576919b66bab3bfcb9ff0913"),
      "Cost" : "$12,619.00"
  }
  
  /* 5 */
  {
      "_id" : ObjectId("576919b66bab3bfcb9ff0914"),
      "Cost" : "$9,250.00"
  }
  </code></pre>