【问题标题】:What is the correct way to query this document? (If the index is correct)查询此文档的正确方法是什么? (如果索引正确)
【发布时间】:2019-09-21 06:36:41
【问题描述】:

我的机器上正在运行一个 BigChainDB docker 容器,我正在尝试存储和检索地理空间数据。 我通过 MongoDB 接口在“元数据”集合中创建了一个 2dsphere 索引“位置”。 我已经检查了命令:

db.people.getIndexes()

而且我觉得一切都还好,其实结果是这样的:

    {
        "v" : 2,
        "key" : {
            "loc" : "2dsphere"
        },
        "name" : "loc_2dsphere",
        "ns" : "bigchain.metadata",
        "2dsphereIndexVersion" : 3
    }

我为尝试一些空间查询而插入的文档是(这是 db.metadata.findOne() 查询的结果):

{
    "_id" : ObjectId("5ccab10a2ce1b70022823a0f"),
    "id" : "752ee9abccf83c7fd25d86c9a7d12229ae292fa27544f6881f1dbf97ccd8b413",
    "metadata" : {
        "location" : {
            "type" : "Point",
            "coordinates" : [
                22.170872,
                113.578749
            ]
        }
    }
}

但是当我使用这个空间查询时,什么都检索不到:

db.metadata.find(
{ 
"metadata": {
     "location": {
       $near: {
         $geometry: {
            type: "Point" ,
            coordinates: [ 22 , 113 ]
         },
       }
     }
  }
})

我做错了什么,还是索引不起作用的可能性?

【问题讨论】:

    标签: mongodb geospatial bigchaindb


    【解决方案1】:

    这里有几个问题。

    首先是索引在字段loc 上,而您的查询正在查询metadata.location

    如果您尝试在 metadata.location 上创建 2dsphere 索引,您将看到第二个错误:

    "errmsg" : "invalid point in geo near query $geometry argument: { type: \"Point\", coordinates: [ 22.0, 113.0 ] }  longitude/latitude is out of bounds, lng: 22 lat: 113",
    

    此错误表明您文档中定义的 GEOJSON 点无效,因为 113 的纬度值是outside the acceptable range of [-90, 90]

    您需要在索引之前将数据更正为有效的 GEOJSON。

    【讨论】:

    • 谢谢,我已经修改了索引,现在是“位置”,我使用了 long 和 lat 值 [44, 11],但是使用相同的查询我没有结果。
    • 我发现了错误,我需要用作嵌套在“元数据”字段中的索引位置,如下所示: db.metadata.createIndex( { metadata.location: "2dsphere" } ) ,问题是这样做我遇到了这个错误: SyntaxError: missing : after property id
    猜你喜欢
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    相关资源
    最近更新 更多