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 elasticsearch, druid, 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.
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).
MongoDB supports three types of geo-spatial indexes
- 2d index –
- 2dsphere index
- geoHaystack 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,
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,
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.
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.
We saw about MongoDB geo-spatial feature, GeoJSON, geohash, geo-spatial queries, geo-spatial indexes.
If you have any queries or if I have missed any concepts, you can mention it in the comment.