The MongoDB extension for geospatial databases

MongoDB supports the GeoJSON object types Point, LineString, Polygon, MiltiPoint, MultiLineString, MultiPolygon, and GeometryCollection. These are used in two-dimensional geometry and geographic surface-of-the-earth data.

Mongo provides a nice tutorial on geospatial databases with an application on restaurant locations in New York City here: https://docs.mongodb.com/manual/tutorial/geospatial-tutorial/.

A GeoJSON object has the form:

<field>: { type: <GeoJSON-type>, coordinates: [longitude, latitude]}

Here, <GeoJSON-type> is one of the seven types listed previously, and longitude and latitude are decimal numbers, with range -180 < longitude < 180 and -90 < latitude < 90. For example, the following is the GeoJSON object for Westminster Abbey in London:

"location": {"type": "Point", "coordinates": [-0.1275, 51.4994]}

Notice that GeoJSON lists longitude before latitude, as with (x, y) coordinates. This is the reverse of the geo URI scheme that lists latitude first, as shown in Figure 10-19:

The MongoDB extension for geospatial databases

Figure 10-19. The Geo URI for Westminster Abbey: latitude before longitude

The code in Figure 10-20 illustrates how we can develop a MongoDB collection of places represented by GeoJSON documents:

The MongoDB extension for geospatial databases

Figure 10-20. Inserting a GeoJSON document into a collection of places