0

I'm using MongoDB Compass v1.26 and I have some collections with lat/long data, but they looks like this:

"coordinates": {
    "latitude": "43.0818",
    "longitude": "-87.892"
}

It does not pick it up in the schema > analyze to show as a map. I think it needs to be an array.

However, using the tool, I can only filter, project, sort, etc. So, I can't run a query to { $objectToArray: <object> } other than storing a converted copy of the entire collection.

ericosg
  • 4,926
  • 4
  • 37
  • 59

1 Answers1

4

If your field contains GeoJSON data or [longitude,latitude] arrays, the Schema tab displays a map containing the points from the field. The data type for location fields is coordinates.

MongoDB supports the GeoJSON object types.

To specify GeoJSON data, use an embedded document with:

  • a field named type that specifies the GeoJSON object type and
  • a field named coordinates that specifies the object's coordinates.

If specifying latitude and longitude coordinates, list the longitude first and then latitude:

coordinates: [<longitude>, <latitude>]

or

<field>: { type: <GeoJSON type> , coordinates: [<longitude>, <latitude>] }

You can try below projection in PROJECT part,

  • $toDouble to convert string to double type
  • pass both long and lat as per first syntax in array
{ coordinates: [{ $toDouble:"$coordinates.longitude"}, {$toDouble:"$coordinates.latitude"}] }
turivishal
  • 34,368
  • 7
  • 36
  • 59