from pymongo import MongoClient, GEOSPHEREclient = MongoClient()collection = db['COLLECTION_NAME']# create indexcollection.create_index([('geo', GEOSPHERE)])# store GPS coordinates in dblat = ...lon = ...item = { 'geo': { 'type': "Point", 'coordinates': [lon, lat] } }collection.insert(item) # query by location with max distancelocation = [lon, lat]distance_in_meter = 1000items = collection.find('geo': {"$nearSphere": [lon, lat], "$maxDistance": distance_in_meter}).limit(100)
References: