Articles → MONGODB → Introduction To Geospatial Queries In Mongodb
Introduction To Geospatial Queries In Mongodb
What Is Geospatial?
What Are Geospatial Queries?
Shapes In Geospatial Queries
- Point → A single position
- LineString → A series of points connected by straight lines
- Polygon → A closed area defined by multiple points
- MultiPoint, MultiLineString, and MultiPolygon→Collections of points, line strings, and polygons, respectively
- GeometryCollection → A collection of different geometric types
Location Document Collection
Creating An Index
db.locations.createIndex({ location: "2dsphere" });
Geospatial Query
db.locations.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [73.8567, 18.5204]
},
$maxDistance: 500 // Distance in meters
}
}
});
Output