mongodb geoNear 使用記錄

須要,查詢一個座標點的附近位置python

數據格式

能夠用數組或者geojson的形式git

<field>: [ <x>, <y> ]
<field>: [<longitude>, <latitude> ]

GeoJSONmongodb

location: {
      type: "Point",
      coordinates: [-73.856077, 40.848447]
}

先列出經度,而後再列出緯度shell

好比 loc 字段json

{
    "city": "北京市",
    "geo_id": 565932,
    "geo_name": "萬柳園(長春堂藥店)",
    "lat": 39.850201868495773283,
    "lng": 116.33426020654366084,
    "loc": "[116.33426020654366084,39.850201868495773283]",
    "status": 0
 }

索引

建立 2dsphere 索引 數組

2dsphere索引支持查詢球面幾何實體對象spa

db.collection.createIndex( { <location field> : "2dsphere" } )

pymongo查詢例子code

lng = geo['lng']
        lat = geo['lat']
        result = geos_collection.aggregate([
            {"$geoNear": { 
                "near": { 
                    "type": "Point",
                    "coordinates": [lng, lat] }, 
                    "distanceField": "distance", 
                    "maxDistance": 2000, 
                    "query": {"status": -1}, 
                    "spherical": True }
            },
            {"$limit": 10}
        ])

mongodb shell對象

db.o2o_geos.aggregate([
   {
     $geoNear: {
        near: { type: "Point", coordinates: [120.13606048541625171, 30.29447292933346958 ] },
        distanceField: "distance",
        maxDistance: 2000,
        query: { status: -1 },
        spherical: true
     }
   }
])
  • coordinates 查詢的座標點
  • maxDistance 最大距離
  • query 過濾條件

links

相關文章
相關標籤/搜索