import config # Self-created for initializing db. You can also ignore it and implement yours. from bson.son import SON from pymongo import GEOSPHERE from typing importList
defsearch_nearby( latitude: float, longitude: float, max_distance: int = 500 ) -> List[dict]: """Search Nearby data Args: latitude (float): Latitude of the search location longitude (float): Longitude of the search location max_distance (int, optional): Search distance (measured in meter). Defaults to 500. Returns: List[dict]: Search result """ result = [] config.db["latest"].create_index([("loc", GEOSPHERE)]) query = { "loc": { "$near": SON( [ ( "$geometry", SON( [ ("type", "Point"), ("coordinates", [longitude, latitude]), ] ), ), ("$maxDistance", max_distance), ] ) } } for each in config.db["latest"].find(query): result.append(each) return result