GeoJSON Objects

Overview

MongoDB supports the GeoJSON object types listed on this page.

To specify GeoJSON data, use an embedded document with:

  • a field named type that specifies the GeoJSON objecttype and

  • a field named coordinates that specifies the object’scoordinates.

If specifying latitude and longitude coordinates, list thelongitude first and then latitude:

  • Valid longitude values are between -180 and 180, bothinclusive.
  • Valid latitude values are between -90 and 90, bothinclusive.
  1. <field>: { type: <GeoJSON type> , coordinates: <coordinates> }

MongoDB geospatial queries on GeoJSON objects calculate on a sphere;MongoDB uses the WGS84 reference system for geospatialqueries on GeoJSON objects.

Point

The following example specifies a GeoJSON Point:

  1. { type: "Point", coordinates: [ 40, 5 ] }

LineString

The following example specifies a GeoJSON LineString:

  1. { type: "LineString", coordinates: [ [ 40, 5 ], [ 41, 6 ] ] }

Polygon

Polygons consist ofan array of GeoJSON LinearRing coordinate arrays. TheseLinearRings are closed LineStrings. Closed LineStrings haveat least four coordinate pairs and specify the same position as thefirst and last coordinates.

The line that joins two points on a curved surface may or may not containthe same set of co-ordinates that joins those two points on a flatsurface. The line that joins two points on a curved surface will be ageodesic. Carefully check points to avoid errors with shared edges, aswell as overlaps and other types of intersections.

Polygons with a Single Ring

The following example specifies a GeoJSON Polygon with an exteriorring and no interior rings (or holes). The first and last coordinatesmust match in order to close the polygon:

  1. {
  2. type: "Polygon",
  3. coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ]
  4. }

For Polygons with a single ring, the ring cannot self-intersect.

Polygons with Multiple Rings

For Polygons with multiple rings:

  • The first described ring must be the exterior ring.
  • The exterior ring cannot self-intersect.
  • Any interior ring must be entirely contained by the outer ring.
  • Interior rings cannot intersect or overlap each other. Interiorrings cannot share an edge.

The following example represents a GeoJSON polygon with an interior ring:

  1. {
  2. type : "Polygon",
  3. coordinates : [
  4. [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ],
  5. [ [ 2 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] , [ 2 , 2 ] ]
  6. ]
  7. }

Diagram of a Polygon with internal ring.

MultiPoint

New in version 2.6: Requires Versions

GeoJSON MultiPointembedded documents encode a list of points.

  1. {
  2. type: "MultiPoint",
  3. coordinates: [
  4. [ -73.9580, 40.8003 ],
  5. [ -73.9498, 40.7968 ],
  6. [ -73.9737, 40.7648 ],
  7. [ -73.9814, 40.7681 ]
  8. ]
  9. }

MultiLineString

New in version 2.6: Requires Versions

The following example specifies a GeoJSON MultiLineString:

  1. {
  2. type: "MultiLineString",
  3. coordinates: [
  4. [ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
  5. [ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
  6. [ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
  7. [ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
  8. ]
  9. }

MultiPolygon

New in version 2.6: Requires Versions

The following example specifies a GeoJSON MultiPolygon:

  1. {
  2. type: "MultiPolygon",
  3. coordinates: [
  4. [ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.9814, 40.7681 ], [ -73.958, 40.8003 ] ] ],
  5. [ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.958, 40.8003 ] ] ]
  6. ]
  7. }

GeometryCollection

New in version 2.6: Requires Versions

The following example stores coordinates of GeoJSON typeGeometryCollection:

  1. {
  2. type: "GeometryCollection",
  3. geometries: [
  4. {
  5. type: "MultiPoint",
  6. coordinates: [
  7. [ -73.9580, 40.8003 ],
  8. [ -73.9498, 40.7968 ],
  9. [ -73.9737, 40.7648 ],
  10. [ -73.9814, 40.7681 ]
  11. ]
  12. },
  13. {
  14. type: "MultiLineString",
  15. coordinates: [
  16. [ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
  17. [ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
  18. [ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
  19. [ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
  20. ]
  21. }
  22. ]
  23. }