Introduction
Geospatial queries help you find places or points near a location. This is useful for maps and location-based apps.
db.collection.find({ location: { $near: { $geometry: { type: "Point", coordinates: [longitude, latitude] }, $maxDistance: distance_in_meters } } })db.places.find({ location: { $near: { $geometry: { type: "Point", coordinates: [-73.97, 40.77] }, $maxDistance: 5000 } } })db.places.find({ location: { $geoWithin: { $centerSphere: [[-73.97, 40.77], 0.01] } } })db.places.createIndex({ location: "2dsphere" })
db.places.insertMany([
{ name: "Cafe", location: { type: "Point", coordinates: [-73.97, 40.77] } },
{ name: "Library", location: { type: "Point", coordinates: [-73.98, 40.78] } },
{ name: "Bookstore", location: { type: "Point", coordinates: [-74.00, 40.75] } }
])
db.places.find({ location: { $near: { $geometry: { type: "Point", coordinates: [-73.97, 40.77] }, $maxDistance: 3000 } } })$near operator do in MongoDB geospatial queries?$near$near operator is used to find documents that are geographically close to a given point.$near functionality$near = Finds nearby points [OK]$near in MongoDB?location, what will this query return?{ location: { $near: { $geometry: { type: 'Point', coordinates: [-73.97, 40.77] }, $maxDistance: 500 } } }{ location: { $near: { $geometry: { type: 'Point', coordinates: [40.77, -73.97] } } } }$near operator with $geometry requires a 2dsphere index on the location field.$near. Coordinate order affects results but does not cause an error.location field is not indexed with 2dsphere -> Option C{ type: 'Point', coordinates: [-122.42, 37.77] }. Which query correctly finds these restaurants assuming a 2dsphere index on location?$near operator with $maxDistance finds points within distance.Pass the test and this pattern moves unknown