【问题标题】:Mongo 3.0.5 - cannot create index on polygonMongo 3.0.5 - 无法在多边形上创建索引
【发布时间】:2015-11-10 13:31:24
【问题描述】:

我在 Mongo 3.0.5 的集合中创建了这个文档

db.s.insert( {_id: "Poly1", shape: {type: "Polygon", coordinates: [[ [3,1], [1,2], [5,6], [9,2], [4,3], [3,1] ]] } })

然后我尝试在其上创建一个 2dshere 索引

db.s.createIndex({"shape.coordinates" : "2dsphere"}, {bits:26});

然后给我这个错误

 "errmsg" : "exception: Can't extract geo keys: { _id: \"Poly1\", shape: { type: \"Polygon\", coordinates: [ [ [ 3.0, 1.0 ], [ 1.0, 2.0 ], [ 5.0, 6.0 ], [ 9.0, 2.0 ], [ 4.0, 3.0 ], [ 3.0, 1.0 ] ] ] } }  Point must only contain numeric elements",

【问题讨论】:

    标签: mongodb indexing geospatial


    【解决方案1】:

    这是与此处相关的 2dsphere indexes 手册页中的例外:

    2dsphere 索引支持存储为 GeoJSON 对象和旧坐标对的数据(另请参阅 2dsphere 索引字段限制)。对于旧坐标对,索引将数据转换为 GeoJSON 点。有关支持的 GeoJSON 对象的详细信息,请参阅 GeoJSON 对象。

    所以这里的两个主要词是“pair”,指的是legacy坐标结构,它可以是一个数组,也可以是一组经纬度的键值。另一个关键词是“Point”,它是传统坐标对的存储方式。事实上,这种形式只有曾经是一个“Point”对象。

    您的数据包含 GeoJSON 格式和 “多边形”,这最终意味着您在“错误的位置”进行索引。请改用 GeoJSON 的根目录:

    db.s.createIndex({"shape" : "2dsphere"});
    

    然后创建索引并将按设计工作。

    另外,我建议不要在索引上使用其他设置,直到您更熟悉它们在此处的工作方式。启动并运行一些查询,然后更改设置并观察效果。

    【讨论】:

      猜你喜欢
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-06
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      相关资源
      最近更新 更多