It would be nice if you could use the GeoJSON object to represent the location, but the types currently supported are actually limited , so the Circle type, which would be ideal, is not supported.
The closest thing you can do is a “polygon” approaching a circle, but it's probably too much work to build a query just for this purpose. Another result with this, and then using $geoIntersects is that the results will not be sorted by distance from the query point. It seems to be the opposite of finding the “nearest pizza” at the origin.
Fortunately, the $geoNear operation has been added to the aggregation platform, starting with MongoDB 2.4 and higher. The good thing here is that it allows the "projection" of the distance field in the results. Then it allows you to perform logical filtering on the server with those points that are within the radius, at a distance from the origin. It also allows you to sort on the server.
But you still need to change the schema to support the index
db.places.insert({ "name": "Pizza Hut", "location": { "type": "Point", "coordinates": [ 151.00211262702942, -33.81696995135973 ] }, "radius": 20 }) db.places.ensureIndex({ "location": "2dsphere" })
And for aggregation request:
db.places.aggregate([
It basically says
* "up to 100 kilometers from this place, find places with their distance from this point. If the distance is within your" delivery radius ", return them sorted by the nearest"
There are other options that you can pass $geoNear to refine the result, as well as return more than the default value. 100, if required, and basically pass on other parameters for the request, such as "type" or "name" or any other information contained in the document.