Home > Blockchain >  How do I convert all geo locations in mongo db from strings to floats?
How do I convert all geo locations in mongo db from strings to floats?

Time:12-08

skatespot-dev> db.parks.findOne();
{
  _id: ObjectId("638f9866d9014fabfc47275e"),
  address: '801 Pine St, Anchorage, AK',
  name: 'Russian Jack Skatepark',
  location: { type: 'Point', coordinates: [ '61.214855', '-149.793563' ] }
}

I need to run a query to convert all coodinates to floats i guess.

CodePudding user response:

You can convert them to double using update with pipeline, like this:

db.collection.updateMany({},
[
  {
    $set: {
      "location.coordinates": [
        {
          $toDouble: {
            $first: "$location.coordinates"
          }
        },
        {
          $toDouble: {
            $last: "$location.coordinates"
          }
        }
      ]
    }
  }
])

See how it works on the playground example

  • Related