0

I've documents that stores data as strings. I would like to convert some of the fields into doubles.

I've already tried parseFloat, which worked, but I'm not interested in such high precision. Is there a way to convert string into a double?

kubawindu
  • 19
  • 4
  • There is a native aggregation method called [$toDouble](https://docs.mongodb.com/manual/reference/operator/aggregation/toDouble/). Are you trying to change the stored value type or only the returned documents? If you provide a sample string and identify the desired degree of precision, someone may be able to share sample code. – It'sNotMe Mar 28 '19 at 16:54
  • Possible duplicate of [how to convert string to numerical values in mongodb](https://stackoverflow.com/questions/29487351/how-to-convert-string-to-numerical-values-in-mongodb) – Ashh Mar 28 '19 at 17:50

1 Answers1

0

I think you want $toDouble.

db.collection.aggregate([
{$group: {_id:{}, 
avg_pop: {$avg: {$toDouble: "$POPULATION"}} } }])
the_cat_lady
  • 852
  • 1
  • 9
  • 16