Assign a delivery to the nearest available driver
Great — you already have clean Mongoose models! To assign a delivery to the nearest available driver , you'll implement a geo-based query on the Driver collection to find a driver close to the restaurant’s location and available. ✅ Step-by-Step Logic 1. Get the Restaurant Location From the Restaurant model: js Copy Edit const restaurant = await Restaurant . findById (restaurantId); const { lat, lng } = restaurant. address . coordinates ; 2. Find the Nearest Available Driver You can use MongoDB's geoNear feature if you convert currentLocation into a 2dsphere index : 🛠 Modify Driver model: js Copy Edit driverSchema. index ({ currentLocation : "2dsphere" }); ✅ This lets MongoDB understand lat/lng for spatial queries. 3. Query for Nearby Available Drivers js Copy Edit const nearbyDrivers = await Driver . find ({ status : 'available' , currentLocation : { $near : { $geometry : { type : "Point" , co...