We know that we can store different data types in a database and can query it. In this post, we will see about storing and querying geo-location data type. There are different data stores like elasticsearchdruid, MongoDB which support geo-spatial feature, where each of this data store has different data models and indexes. I have chosen MongoDB, since I am quite familiar with that.

Geo-Spatial Data

In MongoDB, Geo-Spatial data can be stored in the form of GeoJSON. So what is GeoJSON? GeoJSON is format for encoding a variety of geographical data structures in JSON format. Geographical data structures like Point, LineString, Polygon, MultiPoint, MultiPolygon can be represented in GeoJSON format. MongoDB also supports data representation in the form of Geohash (In this post, Geohash is explained in detail).

Geo-Spatial Indexes

MongoDB supports three types of geo-spatial indexes

  • 2d index
  • 2dsphere index
  • geoHaystack index

2d Index

2d Index is intended for legacy coordinate pairs. It means 2d index is used to query data stored as points in a two dimensional plane. 2d index can be queried using following geo-spatial operators,

  • $near
  • $nearSphere
  • $geoWithin

2dsphere Index

2dsphere index supports queries on any geometries on an earth-like sphere. The data can stored in both the ways, GeoJSON and legacy coordinate pairs.  This index supports all types of geo-spatial operators like,

  • $near
  • $nearSphere
  • $geoWithin
  • $geoIntersects

GeoHaystack Index

GeoHaystack index are used to query over small area. This index improves performance on queries that use flat like geometry. This index create buckets of documents from the same geographic area in order to improve performance for queries limited to that area. Each bucket in a index contains all the documents within a specified proximity to a given longitude and latitude.

Geo-Spatial Queries

MongoDB supports the following geo-spatial queries,

  • $near – Specifies a point for which a geo-spatial query returns the documents from nearest to farthest.
  • $geoWithin – Selects documents with geo-spatial data that exists entirely within a specified shape.
  • $geoIntersects – Selects documents whose geo-spatial data intersects with a specified GeoJSON object; i.e. where the intersection of the data and the specified object is non-empty.
  • $nearSphere – Specifies a point for which a geospatial query returns the documents from nearest to farthest. MongoDB calculates distances for $nearSphere using spherical geometry.


If you have any queries or if I have missed any concepts, you can mention it in the comment.



1 Comment

Geo Spatial analytics with MongoDB – Blog by Rilwan · March 3, 2019 at 10:01 pm

[…] was exploring a similar feature in MongoDB and that is were I end up with the post, where we saw the concepts of Geo Spatial feature in MongoDB. In this post we are going to see a […]

Leave a Reply

%d bloggers like this: