14. Geospatial Indexes
Geospatial indexes support queries involving locations and geometric data. MongoDB's 2dsphere index supports spherical geometry and GeoJSON objects, making it suitable for many real-world location queries.
A typical GeoJSON point uses coordinates in longitude, latitude order. Incorrect coordinate order is a common source of location bugs.
Geospatial queries can find documents within a region or near a location. The required query operator and index type depend on the desired geometry and coordinate representation. Validate coordinates and coordinate reference assumptions before indexing production data.
GeoJSON point coordinates are [longitude, latitude], not [latitude, longitude].
db.stores.createIndex({ location: "2dsphere" });
db.stores.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [80.2707, 13.0827]
},
$maxDistance: 5000
}
}
});- Geospatial indexes support location-based queries.
- 2dsphere is commonly used with GeoJSON data.
- GeoJSON coordinates use longitude then latitude.
- Validate coordinate data before relying on location results.
In a GeoJSON Point, what is the coordinate order?