"MongoError:无法从对象中提取地理键",类型为:Point

TJR*_*TJR 18 javascript mongodb meteor

我尝试使用以下方法准备我的数据库字段以进行地理编码:

MyCollection._ensureIndex({'data.address.located':'2dsphere'});
Run Code Online (Sandbox Code Playgroud)

但是这个错误来了:

MongoError: Can't extract geo keys from object, malformed geometry?:
{ type: "Point", coordinates: [ 32.4586858, -110.8571443 ] }
Run Code Online (Sandbox Code Playgroud)

我看不出这个领域有什么问题?任何的想法 ?

当我去看一下这个就说明了这一点:

The following example stores a GeoJSON Point:

{ loc: { type: "Point", coordinates: [ 40, 5 ] } }
Run Code Online (Sandbox Code Playgroud)

go-*_*leg 52

问题是

[ 32.4586858, -110.8571443 ]
Run Code Online (Sandbox Code Playgroud)

不是有效的坐标.顺序应该是经度,然后是纬度,而该坐标看起来是相反的(通过有效纬度范围是-90到90并且-110.8571443在其之外的事实来判断).

我想你的意思是:

{ type: "Point", coordinates: [ -110.8571443, 32.4586858 ] }
Run Code Online (Sandbox Code Playgroud)

或者还有其他一些输入错误.

  • 它发生在最好的家庭!甚至在我的! (4认同)
  • 当我见过的其他系统首先放入"lat"时,我"喜欢"每个人都为此而自责. (4认同)
  • 该死的......你是对的.这是如此愚蠢......几天前我遇到了同样的问题并修复了它...现在同样的错误.谢谢 ... (3认同)
  • 错误发生了.MongoDB可以有一个更清晰的消息,解释失败原因是经度值无效. (2认同)
  • 一般来说,拉丁语排在第一位。Mongo 应该用简单的语言给出正确的错误。 (2认同)

Pau*_*een 7

正如@ go-oleg提到的,问题是下一个坐标范围是:

  • 经度:(+ - )180°
  • 纬度:(+ - )90°

和指数预计坐标在该范围.

但是,如果您尝试在已导入的数据上应用索引并发现交换坐标,则可能需要将其交换回来而不是重新导入整个集合.为此,您可以使用下一个mongoshell脚本.

假设你已经有了良好GeoJSON的类型Point.

db.coll.find().forEach(function (e) {
    // assuming that `geoJson` is our field containing `coordinates`
    e.geoJson.coordinates = [ // Swapping Lat/Lon
        e.geoJson.coordinates[1], // Longitude goes first
        e.geoJson.coordinates[0] // Latitude goes last
    ];
    db.coll.save(e);
    // Some `print(e.name)` can go here just to understand the progress
});
Run Code Online (Sandbox Code Playgroud)