在 MongoDB 中保存有效的 GeoJSON 时几何格式不正确?

Malformed geometry when saving valid GeoJSON in MongoDB?

我为 MongoDB 的 2dsphere 索引构建了一个 GeoJSON 对象,就像这样(在 PHP 中):

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

除非这在 MongoDB 中不起作用,返回错误:

Can't extract geo keys from object, malformed geometry

为什么不能保存这个有效的 GeoJSON 点?

这是因为 95 不是有效纬度。

您必须在您的应用程序中根据此答案中提到的纬度和经度点范围验证您的点值:

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.

否则,当用户输入一些实际上无效的数据时,MongoDB 将失败并出现写入异常。

关于错误的 lat/long 值,您是正确的。作为 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.

但对于纬度,此行为未定义

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.

而且很可能这只是 php 强制执行它的驱动程序(我认为它很好)。