Mongo 3.0.5 - 无法在多边形上创建索引

Mongo 3.0.5 - cannot create index on polygon

我在 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",

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

The 2dsphere index supports data stored as GeoJSON objects and as legacy coordinate pairs (See also 2dsphere Indexed Field Restrictions). For legacy coordinate pairs, the index converts the data to GeoJSON Point. For details on the supported GeoJSON objects, see GeoJSON Objects.

所以里面的两个主词是"pair",指的是遗留坐标结构,可以是一个数组,也可以是一组键值经度和纬度。另一个关键字是"Point",这是旧坐标对的存储方式。事实上,只有 ever 一个 "Point" 对象。

您的数据包含 GeoJSON 格式和 "Polygon",这最终意味着您在 "wrong place" 中建立索引。改为使用 GeoJSON 的根:

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

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

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