This chapter introduces how MongoDB stores geospatial data. To make use of MongoDB's geospatial querying functionality, we first need to understand how to store geospatial data.
Geospatial data mainly consists of points, lines, and geometric shapes.
A point on a map is defined by its longitude and latitude coordinates. Multiple points form a line, and multiple lines can form various shapes.
In practical applications: The location of a store, "Where am I?", and "Where is the school?" can all be represented using coordinates. Describing the extent of a campus or a shopping mall requires geometric shapes.
GeoJSON Object
MongoDB uses GeoJSON objects to represent geospatial information.
Common GeoJSON types include:
- Point - representing a coordinate point
- LineString - representing a line
- Polygon - representing a polygon
The format for storing GeoJSON objects in MongoDB fields is as follows:
<field>: { type: <geojson type> , coordinates: <coordinates> }
Explanation:
-
- Field name - type - GeoJSON type
- coordinates - An array of coordinate points. The format varies for different GeoJSON types.
Example:
location: {
type: "Point", // type of geospatial data, in this case, it's a coordinate point
coordinates: [-73.856077, 40.848447] // longitude, latitude
}
Explanation: The location field stores geospatial data of the coordinate point type.
Coordinate Point (Point)
Format:
{ type: "Point", coordinates: [ longitude, latitude ] }
Example:
{ type: "Point", coordinates: [ 40, 5 ] }
Line (LineString)
Format:
{ type: "LineString", coordinates: [ point1, point2, ...] }
Example:
{ type: "LineString", coordinates: [ [ 40, 5 ], [ 41, 6 ] ] }
Polygon
Can be composed of one or more lines. Format:
{
type: "Polygon",
coordinates: [
line1,
line2,
....
]
}
Note: The starting and ending coordinates of each line segment must be the same in order to form a closed shape.
Shape Composed of a Single Line Segment
{
type: "Polygon",
coordinates: [
[ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] // Coordinate points of the line segment, note that the first and last coordinates are the same.
]
}
Shape Composed of Multiple Line Segments
{
type : "Polygon",
coordinates : [
[ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ], // Line segment 1
[ [ 2 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] , [ 2 , 2 ] ] // Line segment 2
]
}
Geospatial Index
MongoDB supports two types of geospatial indexes to speed up the querying of GeoJSON data.
2dsphere
This is a spherical geometry index type, which means when calculating the distance between two points, 2dsphere considers the earth as a sphere.
Example of creating a 2dsphere index:
db.collection.createIndex( { location : "2dsphere" } )
Creates an index for the location field.
2d
2D is a type of planar geometry used to calculate the distance between two points as if they were in a plane. Example of creating a 2D index:
db.collection.createIndex( { location : "2d" } )
This creates an index for the location field.
Geospatial Query Types
Geospatial queries are all related to geometric calculations. Below are the query types supported by MongoDB:
- $geoIntersects - Used to match documents with a specific geometric shape intersection.
- $geoWithin - Matches documents contained within a specified geometric area.
- $near - Typically used to query for documents closest to a specified coordinate point.
Note: For more information on geospatial queries, please refer to the following chapters.