0
0
MongodbHow-ToBeginner · 4 min read

How to Store Location Data in MongoDB: Syntax and Examples

To store location data in MongoDB, use the GeoJSON format inside a field, typically with a type and coordinates array. Then create a 2dsphere index on that field to enable geospatial queries.
📐

Syntax

Location data in MongoDB is stored using the GeoJSON format, which requires two main parts:

  • type: Specifies the geometry type, usually Point for a single location.
  • coordinates: An array with longitude and latitude values in that order.

Example structure:

{ "location": { "type": "Point", "coordinates": [longitude, latitude] } }

To enable location queries, create a 2dsphere index on the location field.

mongodb
db.places.createIndex({ location: "2dsphere" })
Output
Index created on: location (2dsphere)
💻

Example

This example shows how to insert a document with location data and create a geospatial index for queries.

mongodb
db.places.insertOne({
  name: "Central Park",
  location: {
    type: "Point",
    coordinates: [-73.9654, 40.7829]
  }
})

db.places.createIndex({ location: "2dsphere" })

// Find places near a point
const nearby = db.places.find({
  location: {
    $near: {
      $geometry: {
        type: "Point",
        coordinates: [-73.97, 40.77]
      },
      $maxDistance: 5000
    }
  }
}).toArray()

printjson(nearby)
Output
[ { "_id": ObjectId("..."), "name": "Central Park", "location": { "type": "Point", "coordinates": [-73.9654, 40.7829] } } ]
⚠️

Common Pitfalls

  • Wrong coordinate order: MongoDB expects [longitude, latitude], not the other way around.
  • Missing 2dsphere index: Without it, geospatial queries won't work or will be slow.
  • Using legacy coordinate pairs: Avoid storing locations as simple arrays without the GeoJSON type field.
mongodb
/* Wrong way: coordinates reversed */
db.places.insertOne({
  location: {
    type: "Point",
    coordinates: [40.7829, -73.9654] // latitude, longitude (incorrect)
  }
})

/* Right way: coordinates correct order */
db.places.insertOne({
  location: {
    type: "Point",
    coordinates: [-73.9654, 40.7829] // longitude, latitude
  }
})
📊

Quick Reference

ConceptDescriptionExample
GeoJSON PointStores a single location with longitude and latitude{"type": "Point", "coordinates": [-73.97, 40.77]}
2dsphere IndexEnables geospatial queries on GeoJSON datadb.collection.createIndex({ location: "2dsphere" })
Coordinates OrderAlways use [longitude, latitude][-73.97, 40.77]
Query NearbyFind documents near a point within distance{ location: { $near: { $geometry: { type: "Point", coordinates: [...] }, $maxDistance: 5000 } } }

Key Takeaways

Store location data in MongoDB using GeoJSON format with type and coordinates fields.
Coordinates must be in [longitude, latitude] order, not reversed.
Create a 2dsphere index on the location field to enable efficient geospatial queries.
Use $near and other geospatial operators to query locations based on distance.
Avoid storing location as plain arrays without GeoJSON structure for compatibility.