6

I'm currently trying to build an 2dsphere index, but the creation seems to fail.

The document on which the index creation fails is valid geojson (according to geojsonlint).

Also as far as I can see it obeys the MongoDB "Polygon" rules.

I would appreciate any help, since I can't figure out why the index creation seems to fail.

Thanks in advance!

db.poly.ensureIndex( { loc: "2dsphere" } )
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "ok" : 0,
        "errmsg" : "Can't extract geo keys from object, malformed geometry?: { _
id: 353, loc: { type: \"Polygon\", coordinates: [ [ [ 8.090732000000001, 53.6379
766 ], [ 8.050639500000001, 53.6250853 ], [ 8.036974600000001, 53.6286108 ], [ 7
.994035500000001, 53.6016978 ], [ 8.0120927, 53.59855020000001 ], [ 8.0102720000
00001, 53.5883803 ], [ 8.023379, 53.5867745 ], [ 8.0148004, 53.5832729 ], [ 8.02
6839500000001, 53.57895840000001 ], [ 8.0271685, 53.5727671 ], [ 8.0432450000000
01, 53.57190120000001 ], [ 8.0386477, 53.565241 ], [ 8.0192488, 53.5609644 ], [
8.030192100000001, 53.5508096 ], [ 8.037298, 53.5565769 ], [ 8.041402400000001,
53.55249540000001 ], [ 8.030647100000001, 53.53854200000001 ], [ 8.0381080000000
01, 53.5275022 ], [ 8.048501400000001, 53.5243656 ], [ 8.051459700000001, 53.509
912 ], [ 8.091510100000001, 53.50258460000001 ], [ 8.153839000000001, 53.5148059
0000001 ], [ 8.1708242, 53.53993010000001 ], [ 8.164240300000001, 53.5287913 ],
[ 8.1562255, 53.531339 ], [ 8.1700993, 53.54524050000001 ], [ 8.150740200000001,
 53.5596328 ], [ 8.1539377, 53.56452330000001 ], [ 8.1408203, 53.58015880000001
], [ 8.155694800000001, 53.5858101 ], [ 8.1496093, 53.60191990000001 ], [ 8.1234
503, 53.5984032 ], [ 8.090732000000001, 53.6379766 ] ] ] } }",
        "code" : 16755
}
4

4 回答 4

2

像您所做的那样将索引应用于“loc”(GeoJSON 字段)。但是,请确保您的坐标按经度、纬度(不是纬度、经度)的顺序指定。

根据http://docs.mongodb.org/manual/applications/geospatial-indexes/#spherical上的文档:

使用以下坐标轴顺序将您的位置数据存储为 GeoJSON 对象:经度、纬度。GeoJSON 的坐标参考系统使用 WGS84 基准。

此外,根据http://geojson.org/geojson-spec.html#positions上的 GeoJSON 规范:

位置由数字数组表示。必须至少有两个元素,并且可能更多。元素的顺序必须遵循 x、y、z 顺序(投影坐标参考系中的坐标为东、北、高,或地理坐标参考系中的坐标为经度、纬度、高度)。允许任何数量的附加元素——附加元素的解释和含义超出了本规范的范围。

于 2014-12-16T03:46:33.427 回答
2

坐标中有一个路口,但是通过geojsonlint.com看不到,因为不明显!

MongoDB 不允许插入线相交的多边形。

于 2016-09-09T10:58:53.283 回答
0

如果您的集合中有多个文档,并且其中至少一个文档的“loc”字段为 NULL 或 EMPTY,则会发生此错误。

因此,请确保您没有任何“loc”字段为 NULL 或 EMPTY 的文档。

查询:db.poly.find("loc":{$exists:true, $eq:""});

注意:您需要将索引添加到“loc”字段,而不是“loc.coordinates”字段。

于 2016-07-28T06:16:53.997 回答
-2

所以解决方案是简单地建立索引loc.coordinates而不是loc

db.coor.ensureIndex({"loc.coordinates": "2dsphere"})

注意 2d 索引支持不同的格式,您可以在文档中找到示例。虽然loc不是其中之一。

我不明白为什么你在那里有嵌套数组。

于 2014-05-29T01:36:25.093 回答