MongoDB
 SQL >> Base de Dados >  >> NoSQL >> MongoDB

MongoDB: converter tipo string para float

O problema é que toFixed retorna String, não Number. Você precisa converter novamente com parseFloat:

db.MyCollection.find({"ProjectID": 44, "Cost": {$exists: true}}).forEach(function(doc){ var newCost = doc.Cost.replace(/,/g, '').replace(/\$/g, ''); var costString = parseFloat(newCost).toFixed(2); doc.Cost = parseFloat(costString); db.MyCollection.save(doc); })