Yes, query the location near a coordinate point

The data format

This 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": "Beijing "," GEO_ID ": 565932, "GEO_NAME ":" Wanliuyuan (Changchun drugstore)", "LAT ": 39.850201868495773283," LNG ": 116.33426020654366084, "loc" : "[116.33426020654366084, 39.850201868495773283]", "status" : 0}

The index

Create a 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: }, distanceField: "distance", maxDistance: 2000, query: {status: -1 }, spherical: true } } ])
  • Coordinates Point for the Coordinates query
  • MaxDistance maximum distance
  • Query Filter Conditions

links

  • https://docs.mongodb.com/manu…
  • https://docs.mongodb.com/manu…
  • https://docs.mongodb.com/manu…