A geospatial index lets MongoDB answer the questions a map asks: what is near this point, and what falls inside this area. Without one, "find the ten closest stores" is a full scan plus distance math on every document. With a 2dsphere index, it is a fast, sorted lookup. Here is how to set it up, and the one mistake that makes every result wrong.
Create the index and store GeoJSON
Use a 2dsphere index on the field that holds the location:
db.places.createIndex({ location: "2dsphere" })
Then store each location as a GeoJSON object, not a bare pair of numbers:
db.places.insertOne({
name: "Glacier Coffee",
location: { type: "Point", coordinates: [-122.4194, 37.7749] }
})
That is the standard shape the index understands, and it opens up the geospatial query operators. The 2dsphere docs cover the other GeoJSON types (lines, polygons) when you need them.
Longitude first. Always. This is the trap.
Look closely at that coordinate array: [-122.4194, 37.7749] is [longitude, latitude]. GeoJSON puts longitude first, which is the opposite of how people say it ("lat, long") and the opposite of what most map UIs hand you. Swap them and your San Francisco coffee shop lands in the Indian Ocean, and your $near query returns nothing that makes sense.
If your geospatial results look wrong, check the coordinate order before you check anything else. It is the geospatial equivalent of an off-by-one, and it has cost more debugging hours than it has any right to. Longitude, then latitude. Tape it to the monitor.
Find what is near
With the index in place, $near returns documents sorted nearest first, with a distance cap in meters:
db.places.find({
location: {
$near: {
$geometry: { type: "Point", coordinates: [-122.4194, 37.7749] },
$maxDistance: 1000 // within 1 km
}
}
})
To find everything inside a region rather than near a point, $geoWithin takes a polygon and does not sort by distance, which makes it the right tool for "every store in this delivery zone."
2dsphere, not 2d
MongoDB has an older 2d index for flat coordinate pairs. Unless you are working with a legacy schema, ignore it and use 2dsphere, which models the earth as a sphere and gets distances right across real geography. "Geospatial index" in 2026 means 2dsphere.
It still lives in your index budget
A geospatial index is a real index: it speeds up location queries and it costs writes like any other. So it belongs in the same index optimization accounting as the rest, kept if your queries use it and pruned if they do not. The general syntax and options are in how to create an index, and if a location query is slow even with the index, read the plan.
mongoui's Index view lists every index including the geospatial ones, with how often each is actually queried, so a 2dsphere you built for a feature that shipped and then got cut does not keep taxing your writes unnoticed. It runs on your machine, so your location data never leaves your network. Create the 2dsphere, put longitude first, and let MongoDB do the distance math.
