Convert string to number

Note that you don’t need to modify the documents in the shell, you can use $toDouble (or any other conversion function) in update command to modify the field.

Example:

     db.test.update({}, [ {$set:{ "tax rate" : {$toDouble: "$tax rate"}}} ], {multi:true})

will do the same thing your script is doing without bringing documents to the client and without potential race conditions. You can use $toInt, $toLong or $convert if you want to specify more options.

4 Likes