I am designing a cab booking service using MongoDB. My schema looks like -
{
"_id": {
"$oid": "621ca96e2c7e7efcfba6ce34"
},
"registration_id": "Id1",
"location": [{
"$numberDouble": "13.197827893722762"
}, {
"$numberDouble": "77.70575957707639"
}]
}
where location is Point GeoJson datatype.
Based on customer's location, I have to provide nearest cab when he makes request. I am using $near query for the same.
query = {"location": { "$near": { "$geometry": { "type": "Point", "coordinates": [event['longitude'], event['latitude']] }}}}
However I need to provide only cabs that belong to given area, defined by 4 co-ordinates.
If I use $geowithin, which provides points defined by area, i wont get sorted results, if I use $near like above, I am not able to limit area.
What is the elegant way to achieve this?
This is very similar to Determine distance from coordinates after aggregation match but the nuance is that it might not be obvious that the
queryoption for$geoNearcan itself supportgeoWithin: