【问题标题】:mongodb geoNear for arrays of locationsmongodb geoNear 用于位置数组
【发布时间】:2014-02-25 16:57:00
【问题描述】:

我有这样的文件:

{positions: [ [0.1, 0.1], [0.2, 0.2] ]}

我想查询点[0.11, 0.11]附近的文档。像这样的:

{positions: {$near: [0.11, 0.11]} }

如何添加二维索引以及如何查询?谢谢。

【问题讨论】:

  • MongoDB 手册中包含不同索引类型的geospatial tutorials。对于2d 索引,请注意坐标顺序必须始终为[longitude,latitude]。鉴于您的值看起来不像 long/lat,如果您使用不同的坐标系,您可能需要 adjust the coordinate ranges,因为默认范围是 -180 到 180(即假定 long/lat)。

标签: mongodb indexing geolocation mongodb-query


【解决方案1】:

虽然您的示例数据不是最好的,但索引创建和查询非常简单。要创建 2d 索引,您只需要以下命令:

db.collection.ensureIndex( { positions: "2d" } )

您在数组中的数组数据格式非常有效。查询很简单,基本语法如下:

db.collection.find({ positions: {$near: [-75, 111]} })

唯一需要注意的是,如果不进行任何投影,您将返回整个文档,包括整个位置数组,并且该结果将针对数组中的任何匹配位置重复。

作为新手,您最好阅读一些有关 MongoDB 地理空间功能的资料,并决定哪种解决方案最适合您的案例。以下链接是一个很好的起点:

http://docs.mongodb.org/manual/core/geospatial-indexes/#store-location-data

http://docs.mongodb.org/manual/administration/indexes-geo/

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2018-08-11
    • 2015-11-18
    • 1970-01-01
    • 2018-08-25
    相关资源
    最近更新 更多