1

Need, query the nearby position of a coordinate point

Data Format

Can be in the form of an array or geojson

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

GeoJSON

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

List the longitude first, then the latitude

For example, the loc field

  {
    "city": "北京市",
    "geo_id": 565932,
    "geo_name": "万柳园(长春堂药店)",
    "lat": 39.850201868495773283,
    "lng": 116.33426020654366084,
    "loc": "[116.33426020654366084,39.850201868495773283]",
    "status": 0
 }

index

Create 2dsphere index

2dsphere index supports querying spherical geometric entity objects

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

pymongo query example

        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 Query coordinate point
  • maxDistance maximum distance
  • query filter condition

links


lidashuang
6.7k 声望165 粉丝

$ Ruby/Elixir/Golang