Mongodb Query Gps/Geospatial Distance (Python)

Sep 9, 2020
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:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.