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

新手上路,请多包涵

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

 MyCollection._ensureIndex({'data.address.located':'2dsphere'});

但是随后出现此错误:

 MongoError: Can't extract geo keys from object, malformed geometry?:
{ type: "Point", coordinates: [ 32.4586858, -110.8571443 ] }

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

当我看 这个 时,它显示了这个:

 The following example stores a GeoJSON Point:

{ loc: { type: "Point", coordinates: [ 40, 5 ] } }

原文由 TJR 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 481
2 个回答

问题是

[ 32.4586858, -110.8571443 ]

不是有效坐标。顺序应该是经度后跟纬度,而该坐标似乎是相反的(根据有效纬度范围是 -90 到 90 而 -110.8571443 超出该范围这一事实来判断)。

我想你的意思是:

 { type: "Point", coordinates: [ -110.8571443, 32.4586858 ] }

或者有一些其他的输入错误。

原文由 go-oleg 发布,翻译遵循 CC BY-SA 3.0 许可协议

正如 @go-oleg 所提到的,问题是接下来是坐标范围:

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

并且 index 期望 坐标 在该范围内

但是,如果您尝试对 已导入的数据 应用索引并发现 坐标被交换,您可能希望将其交换回来而不是重新导入整个集合。为此,您可以使用下一个 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
});

原文由 Paul T. Rawkeen 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题