简体   繁体   English

MongoDB:在一个查询中使用 $geoIntersects 或 $geoWithin 和 $near

[英]MongoDB: Using $geoIntersects or $geoWithin with $near in one query

I would like to query for all documents that have a polygon that a point is contained in and then for that result set, order it based on closeness of that point to the location of the document.我想查询所有包含一个点的多边形的文档,然后对于该结果集,根据该点与文档位置的接近程度对其进行排序。

So imagine I have a database of friends because I'm just that cool, and would like to see which friends are within the my range and would be willing to come play.所以想象一下,我有一个朋友数据库,因为我就是那么酷,并且想看看哪些朋友在我的范围内并且愿意来玩。 (each friend has a play-date polygon which is the range they are willing to travel for a play-date) (每个朋友都有一个游戏日期多边形,这是他们愿意为一个游戏日期旅行的范围)

For all matches I would like to them proceed to see which friend I should call to come based on his actual address and its distance to my point (which is my address) so that I can determine if I am ok with them coming from far away.对于所有比赛,我希望他们继续根据他的实际地址及其与我的点(这是我的地址)的距离来查看我应该打电话给哪个朋友,以便我可以确定我是否可以接受他们从远处来. (lets say 300 meters) (比如说300米)

So far I have below a query to find polygons that my point is contained within but I do not know how to include the $near operator of mongodb到目前为止,我有一个查询来查找包含我的点的多边形,但我不知道如何包含 mongodb 的 $near 运算符

For JSON:对于 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 }
}

This works:这有效:

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

This does not:这不会:

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
      } 
    }
  }
])

Please help me with the query above that does not work.请帮我解决上面不起作用的查询。

This requires an aggregate pipeline .这需要一个聚合管道 As per mogodb doc for $geoNear , You can only use $geoNear as the first stage of a pipeline.根据$geoNear 的 mogodb 文档,您只能使用 $geoNear 作为管道的第一阶段。 The aggregate function has an entry for an additional query which is where the polygon query will be used to narraw down results based on inclusion in the PLAYDATE_RANGE field of the document.聚合函数有一个附加查询的条目,多边形查询将用于根据文档的 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

     }
   }
])

PS note that only one geospatial index can be used so put it on the FRIEND_POSITION field. PS 请注意,只能使用一个地理空间索引,因此请将其放在 FRIEND_POSITION 字段中。 If adding a 2sphere index that requires a correctly formed GeoJSON value, specifically,如果添加需要正确形成的 GeoJSON 值的 2sphere 索引,特别是,

"FRIEND_POSITION" : { "type" : "Point", "coordinates" : [ -73.992188, 40.729359 ] } “FRIEND_POSITION”:{“类型”:“点”,“坐标”:[-73.992188,40.729359]}

So the document should look like:所以文件应该是这样的:

{
  "_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 ]
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM