-1

I make up a GeoJSON object for my MongoDB's 2dsphere index like so (in PHP):

array (size=2)
  'type' => string 'Point' (length=5)
  'coordinates' => 
    array (size=2)
      0 => float 45
      1 => float 95

Except this does not work in MongoDB, returning an error of:

Can't extract geo keys from object, malformed geometry

Why cannot not save this valid GeoJSON point?

Sammaye
  • 43,242
  • 7
  • 104
  • 146

2 Answers2

1

You are correct about the wrong lat/long values. As per documentation:

By default, a 2d index assumes longitude and latitude and has boundaries of -180 inclusive and 180 non-inclusive. If documents contain coordinate data outside of the specified range, MongoDB returns an error.

But for lat this behavior is not defined

IMPORTANT The default boundaries allow applications to insert documents with invalid latitudes greater than 90 or less than -90. The behavior of geospatial queries with such invalid points is not defined.

And most probably this is just php driver which enforces it (and I think it is good).

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
0

It is because 95 is not a valid latitude.

You must, in your application, validate the values of your point against the range of latitude and longitude points as mentioned in this answer: https://stackoverflow.com/a/16743805/383478

The valid range of latitude in degrees is -90 and +90 for the southern and northern hemisphere respectively. Longitude is in the range -180 and +180 specifying the east-west position.

Otherwise MongoDB will fail with a write exception when the user enters some data that is not actually valid.

Community
  • 1
  • 1
Sammaye
  • 43,242
  • 7
  • 104
  • 146