簡體   English   中英

MongoDB:在一個查詢中使用 $geoIntersects 或 $geoWithin 和 $near

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

我想查詢所有包含一個點的多邊形的文檔,然后對於該結果集,根據該點與文檔位置的接近程度對其進行排序。

所以想象一下,我有一個朋友數據庫,因為我就是那么酷,並且想看看哪些朋友在我的范圍內並且願意來玩。 (每個朋友都有一個游戲日期多邊形,這是他們願意為一個游戲日期旅行的范圍)

對於所有比賽,我希望他們繼續根據他的實際地址及其與我的點(這是我的地址)的距離來查看我應該打電話給哪個朋友,以便我可以確定我是否可以接受他們從遠處來. (比如說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
      } 
    }
  }
])

請幫我解決上面不起作用的查詢。

這需要一個聚合管道 根據$geoNear 的 mogodb 文檔,您只能使用 $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

     }
   }
])

PS 請注意,只能使用一個地理空間索引,因此請將其放在 FRIEND_POSITION 字段中。 如果添加需要正確形成的 GeoJSON 值的 2sphere 索引,特別是,

“FRIEND_POSITION”:{“類型”:“點”,“坐標”:[-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 ]
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM