import pymongo
client = pymongo.MongoClient("mongodb+srv://admin:admin@cluster0.f7buy.mongodb.net/task?retryWrites=true&w=majority")
db = client.test
mydb = client["task"]
information = mydb.car_rental
record = [{'ID': 1,
  'driver': 116,
  'loc': {'coordinates': [-130.63113013360402, 80.90806656727281],
   'type': 'Point'},
  'number_plate': 'gj07-70fu96'},
 {'ID': 18,
  'driver': 101,
  'loc': {'coordinates': [155.7343666933789, -9.0158159736107],
   'type': 'Point'},
  'number_plate': 'gj09-2ll13'},
 {'ID': 9,
  'driver': 105,
  'loc': {'coordinates': [70.85453813636406, 14.11013058968372],
   'type': 'Point'},
  'number_plate': 'gj01-76mk10'},
 {'ID': 14,
  'driver': 126,
  'loc': {'coordinates': [-156.2399378200575, -16.535375502969387],
   'type': 'Point'},
  'number_plate': 'gj03-91io23'},
 {'ID': 16,
  'driver': 148,
  'loc': {'coordinates': [91.9666989368879, -47.33514355625501],
   'type': 'Point'},
  'number_plate': 'gj08-21js81'},
 {'ID': 11,
  'driver': 135,
  'loc': {'coordinates': [91.39542784604868, 72.60831370120567],
   'type': 'Point'},
  'number_plate': 'gj08-75le7'},
 {'ID': 9,
  'driver': 124,
  'loc': {'coordinates': [152.28883279673403, 13.262307601011457],
   'type': 'Point'},
  'number_plate': 'gj08-7fx24'},
 {'ID': 8,
  'driver': 108,
  'loc': {'coordinates': [76.14891310624984, -89.48334113439138],
   'type': 'Point'},
  'number_plate': 'gj04-77px74'},
 {'ID': 4,
  'driver': 127,
  'loc': {'coordinates': [-135.26245369002072, 21.0860844517303],
   'type': 'Point'},
  'number_plate': 'gj01-72aj92'},
 {'ID': 7,
  'driver': 126,
  'loc': {'coordinates': [-172.8431518176736, -31.203917681022695],
   'type': 'Point'},
  'number_plate': 'gj09-75qr20'},
 {'ID': 18,
  'driver': 141,
  'loc': {'coordinates': [-170.56404472402107, 4.8704584063696785],
   'type': 'Point'},
  'number_plate': 'gj08-76bx32'},
 {'ID': 4,
  'driver': 123,
  'loc': {'coordinates': [-169.80966431251395, 69.46957787593942],
   'type': 'Point'},
  'number_plate': 'gj08-43ma12'},
 {'ID': 5,
  'driver': 110,
  'loc': {'coordinates': [-91.36401566290348, -25.691443199550037],
   'type': 'Point'},
  'number_plate': 'gj02-20hw65'},
 {'ID': 18,
  'driver': 104,
  'loc': {'coordinates': [129.61006152583968, 73.53632716556385],
   'type': 'Point'},
  'number_plate': 'gj02-30nn94'},
 {'ID': 12,
  'driver': 140,
  'loc': {'coordinates': [13.979183162995014, 22.204820842524498],
   'type': 'Point'},
  'number_plate': 'gj04-36gj60'},
 {'ID': 13,
  'driver': 124,
  'loc': {'coordinates': [19.015912326875707, -87.15217628761987],
   'type': 'Point'},
  'number_plate': 'gj01-7zj24'},
 {'ID': 17,
  'driver': 137,
  'loc': {'coordinates': [123.13112124945803, 77.29870207387702],
   'type': 'Point'},
  'number_plate': 'gj09-45sh82'},
 {'ID': 14,
  'driver': 128,
  'loc': {'coordinates': [-135.297606556832, 34.361445153046304],
   'type': 'Point'},
  'number_plate': 'gj07-68uw97'},
 {'ID': 10,
  'driver': 138,
  'loc': {'coordinates': [-79.49387555598977, 89.51811308126648],
   'type': 'Point'},
  'number_plate': 'gj05-57fz2'},
 {'ID': 19,
  'driver': 120,
  'loc': {'coordinates': [30.873337724983628, -58.97870849784086],
   'type': 'Point'},
  'number_plate': 'gj06-82sb92'}]
information.insert_many(record)
information.create_index([('loc.coordinates', pymongo.GEO2D)],name='loc')



cars = information.find({"loc.coordinates":{"$near":[-54,63]}}).limit(3)
data =[]

for a in cars:
  data.append({"id":a['driver'],'lon':a['loc']['coordinates'][0],'lat':a['loc']['coordinates'][0]})

print(data)

