【问题标题】:MongoDB: Using $geoIntersects or $geoWithin with $near in one queryMongoDB:在一个查询中使用 $geoIntersects 或 $geoWithin 和 $near
【发布时间】:2022-03-30 21:49:15
【问题描述】:

我想查询具有包含点的多边形的所有文档,然后针对该结果集,根据该点与文档位置的接近程度对其进行排序。

所以想象一下,我有一个朋友数据库,因为我就是这么酷,并且想看看哪些朋友在我的范围内并且愿意来玩。 (每个朋友都有一个游戏日期多边形,这是他们愿意为游戏日期旅行的范围)

对于所有比赛,我希望他们根据他的实际地址及其与我的地点(即我的地址)的距离(即我的地址)继续查看我应该打电话给哪个朋友,以便我可以确定我是否可以接受他们的到来从遥远的地方。 (假设是 300 米)

到目前为止,我有一个查询来查找包含我的点的多边形,但我不知道如何包含 mongodb 的 $near 运算符

对于 JSON:

{
    "_id" : "objid",
    "FRIEND_NAME" : "Bobby",
    "GEOMETRY" : {
        "type":"Polygon",
        "coordinates":[[
            [-73.98779153823898,40.718233223261],
            [-74.004946447098,40.723575517498],
            [-74.006771211624,40.730592217474],
            [-73.99010896682698,40.746712376146],
            [-73.973135948181,40.73974615047701],
            [-73.975120782852,40.736128627654],
            [-73.973997695541,40.730787341083],
            [-73.983317613602,40.716639396436],
            [-73.98779153823898,40.718233223261]
    ]]},
    "FRIEND_POSITON" : {"lon" : -73.992188, "lat" : 40.729359 }
}

这行得通:

db.friends.find({
  "PLAYDATE_RANGE":{
    "$geoIntersects":{
      "$geometry":{
        "type":"Point",
        "coordinates":[-73.98652, 40.752044]
      }
    }
  }
})

这不是:

db.friends.find([
  {
    "PLAYDATE_RANGE":{
      "$geoIntersects":{
        "$geometry":{
          "type":"Point",
          "coordinates":[-73.98652, 40.752044]
        }
      }
    }
  },
  {
    "FRIEND_POSITON":{
      "$geoNear":{
        "near":{
          "type":"Point",
          "coordinates": [-73.98652, 40.752044]
        },
        "maxDistance":300
      } 
    }
  }
])

请帮我解决上面不起作用的查询。

【问题讨论】:

    标签: mongodb geospatial


    【解决方案1】:

    这需要aggregate pipeline。根据mogodb doc for $geoNear,您只能使用 $geoNear 作为管道的第一阶段。聚合函数有一个附加查询条目,在该条目中,多边形查询将用于根据文档的 PLAYDATE_RANGE 字段中包含的内容来缩小结果。

    db.friends.aggregate([
        {
            $geoNear: {
                near: { type: "Point", coordinates: [-73.98652, 40.752044] },
                maxDistance: 300,
                distanceField: "friends.calculated_distance",
                query: {
                    "PLAYDATE_RANGE": {
                        "$geoIntersects": {
                            "$geometry": {
                                "type": "Point",
                                "coordinates":[-73.98652, 40.752044]
                            }
                        }
                    }
                },
                spherical: true
            }
        }
    ])
    

    附:请注意,只能使用一个地理空间索引,因此请将其放在 FRIEND_POSITION 字段中。如果添加需要正确格式的 GeoJSON 值的 2sphere 索引,具体来说,

    "FRIEND_POSITION" : { "type" : "Point", "coordinates" : [ -73.992188, 40.729359 ] }

    所以文档应该是这样的:

    {
      "_id" : "objid",
      "FRIEND_NAME" : "Bobby",
      "GEOMETRY" : {
        "type": "Polygon",
        "coordinates":[[
          [-73.98779153823898,40.718233223261],
          [-74.004946447098,40.723575517498],
          [-74.006771211624,40.730592217474],
          [-73.99010896682698,40.746712376146,
          [-73.973135948181,40.73974615047701],
          [-73.975120782852,40.736128627654],
          [-73.973997695541,40.730787341083],
          [-73.983317613602,40.716639396436],
          [-73.98779153823898,40.718233223261]
      ]]},
      "FRIEND_POSITION" : {
        "type" : "Point",
        "coordinates" : [ -73.992188, 40.729359 ]
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 1970-01-01
      • 2015-12-12
      • 2015-07-07
      • 2015-11-01
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 2014-03-06
      相关资源
      最近更新 更多