Explore Related Tools
Hand-picked utilities and calculators related to this tool.
MongoDB allows you to store and query location data with high efficiency. This is essential for apps like Uber, Foodpanda, or any store locator.
Geospatial data is stored using GeoJSON objects. The most common is the "Point".
// Example of a location point (Longitude first, then Latitude)
{
name: "My Store",
location: {
type: "Point",
coordinates: [ -73.856077, 40.848447 ]
}
}To perform proximity queries, you MUST create a 2dsphere index.
db.places.createIndex({ location: "2dsphere" })The `$near` operator returns documents from nearest to farthest.
db.places.find({
location: {
$near: {
$geometry: { type: "Point", coordinates: [ -73.96, 40.78 ] },
$maxDistance: 5000 // Distance in meters (5km)
}
}
})Unlike `$near`, `$geoWithin` does not sort by distance. It just finds all points within a specific area.
db.places.find({
location: {
$geoWithin: {
$centerSphere: [ [ -73.96, 40.78 ], 5 / 3963.2 ] // 5 miles radius
}
}
})Hand-picked utilities and calculators related to this tool.
