【问题标题】:Index for Geometry does not work as expected几何索引未按预期工作
【发布时间】:2014-05-29 22:08:14
【问题描述】:

这是我的命令行的输出。 您可以看到第一次尝试创建索引失败。 然后我只是将 loc-Field 重命名为坐标,它可以工作??? 有人可以向我解释一下吗。这似乎是一种奇怪的行为。

> db.nodes.find().limit(3)
{ "_id" : ObjectId("534d36b682beda5978db27c1"), "geo" : { "type" : "Point", "loc" : [ 9.7366511, 52.3711883 ] } }
{ "_id" : ObjectId("534d36b682beda5978db27c2"), "geo" : { "type" : "Point", "loc" : [ 9.7399576, 52.3691615 ] } }
{ "_id" : ObjectId("534d36b682beda5978db27c3"), "geo" : { "type" : "Point", "loc" : [ 9.7346094, 52.371738 ] } }
> db.nodes.ensureIndex({"geo":"2dsphere"})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "ok" : 0,
        "errmsg" : "Can't extract geo keys from object, malformed geometry?: { _id: ObjectId('534d36b682beda5978db27c1'), geo: { type: \"Point\", loc: [ 9.7366511, 52.3711883 ] } }",
        "code" : 16755
}
> db.nodes.find().limit(3)
{ "_id" : ObjectId("534d36ea82be11ca6e9fb112"), "geo" : { "type" : "Point", "coordinates" : [ 9.7366511, 52.3711883 ] } }
{ "_id" : ObjectId("534d36ea82be11ca6e9fb113"), "geo" : { "type" : "Point", "coordinates" : [ 9.7399576, 52.3691615 ] } }
{ "_id" : ObjectId("534d36ea82be11ca6e9fb114"), "geo" : { "type" : "Point", "coordinates" : [ 9.7346094, 52.371738 ] } }
> db.nodes.ensureIndex({"geo":"2dsphere"})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}

【问题讨论】:

    标签: mongodb indexing geometry geospatial geo


    【解决方案1】:

    不清楚为什么第二次尝试静默失败,但这里的主要问题是您的 json 结构并不完全符合 GeoJSON 规范。 reference

    “Point”GeoJSON 结构的坐标应包含在名为“coordinates”的键中,而不是“loc”中。

    尝试创建索引 { "_id" : ObjectId("534d36b682beda5978db27c3"), "geo" : { "type" : "Point", "coordinates" : [ 9.7346094, 52.371738 ] } }

    请记住,经度总是在坐标数组中排在第一位! :]

    【讨论】:

      猜你喜欢
      • 2019-04-18
      • 1970-01-01
      • 2013-06-02
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 2016-07-01
      • 2014-08-01
      • 2012-08-13
      相关资源
      最近更新 更多