Database Manual / CRUD Operations / Geospatial Queries

GeoJSON Objects对象

Overview概述

MongoDB supports the GeoJSON object types listed on this page.MongoDB支持本页列出的GeoJSON对象类型。

To specify GeoJSON data, use an embedded document with:要指定GeoJSON数据,请使用具有以下功能的嵌入式文档:

  • a field named type that specifies the GeoJSON object type, and一个名为type的字段,指定GeoJSON对象类型,以及
  • a field named coordinates that specifies the object's coordinates.一个名为coordinates的字段,指定对象的坐标。
<field>: { type: <GeoJSON type> , coordinates: <coordinates> }

Important

If specifying latitude and longitude coordinates, list the longitude first, and then latitude.如果指定纬度和经度坐标,请先列出经度,然后再列出纬度

  • Valid longitude values are between -180 and 180, both inclusive.有效的经度值介于-180180之间,包括-180180
  • Valid latitude values are between -90 and 90, both inclusive.有效的纬度值介于-9090之间,包括-9090

MongoDB geospatial queries on GeoJSON objects calculate on a sphere; MongoDB uses the WGS84 reference system for geospatial queries on GeoJSON objects.在球体上计算GeoJSON对象的MongoDB地理空间查询;MongoDB使用WGS84参考系统对GeoJSON对象进行地理空间查询。

Point

The following example specifies a GeoJSON Point:以下示例指定了一个GeoJSON

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

LineString

The following example specifies a GeoJSON LineString:以下示例指定了GeoJSON LineString

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

Polygon

Polygons多边形 consist of an array of GeoJSON LinearRing coordinate arrays. 由GeoJSON LinearRing坐标数组组成。These LinearRings are closed LineStrings. Closed LineStrings have at least four coordinate pairs and specify the same position as the first and last coordinates.这些线性环是封闭的线串。闭线字符串至少有四个坐标对,并指定与第一个和最后一个坐标相同的位置。

The line that joins two points on a curved surface may or may not contain the same set of co-ordinates that joins those two points on a flat surface. The line that joins two points on a curved surface will be a geodesic. Carefully check points to avoid errors with shared edges, as well as overlaps and other types of intersections.连接曲面上两点的线可能包含也可能不包含连接平面上这两点的同一组坐标。连接曲面上两点的线将是测地线。仔细检查点,以避免共享边、重叠和其他类型的交点出现错误。

Polygons with a Single Ring单环多边形

The following example specifies a GeoJSON Polygon with an exterior ring and no interior rings (or holes). The first and last coordinates must match in order to close the polygon:以下示例指定了一个具有外部环而没有内部环(或孔)的GeoJSON多边形。第一个和最后一个坐标必须匹配才能闭合多边形:

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

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. Interior rings cannot share an edge.内环不能相互交叉或重叠。内环不能共享一条边。

The following example represents a GeoJSON polygon with an interior ring:以下示例表示具有内环的GeoJSON多边形:

{
type : "Polygon",
coordinates : [
[ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ],
[ [ 2 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] , [ 2 , 2 ] ]
]
}
Diagram of a Polygon with internal ring.

MultiPoint

Requires 2dsphere Indexes.需要2dsphere索引

GeoJSON MultiPoint embedded documents encode a list of points.GeoJSON多点嵌入式文档对点列表进行编码。

{
type: "MultiPoint",
coordinates: [
[ -73.9580, 40.8003 ],
[ -73.9498, 40.7968 ],
[ -73.9737, 40.7648 ],
[ -73.9814, 40.7681 ]
]
}

MultiLineString

Requires 2dsphere Indexes.需要2dsphere索引

The following example specifies a GeoJSON MultiLineString:以下示例指定了GeoJSON MultiLineString

{
type: "MultiLineString",
coordinates: [
[ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
[ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
[ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
[ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
]
}

MultiPolygon

Requires 2dsphere Indexes.需要2dsphere索引

The following example specifies a GeoJSON MultiPolygon:以下示例指定了GeoJSON多多边形

{
type: "MultiPolygon",
coordinates: [
[ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.9814, 40.7681 ], [ -73.958, 40.8003 ] ] ],
[ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.958, 40.8003 ] ] ]
]
}

GeometryCollection

Requires 2dsphere Indexes.需要2dsphere索引

The following example stores coordinates of GeoJSON type GeometryCollection:以下示例存储GeoJSON类型GeometricCollection的坐标:

{
type: "GeometryCollection",
geometries: [
{
type: "MultiPoint",
coordinates: [
[ -73.9580, 40.8003 ],
[ -73.9498, 40.7968 ],
[ -73.9737, 40.7648 ],
[ -73.9814, 40.7681 ]
]
},
{
type: "MultiLineString",
coordinates: [
[ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
[ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
[ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
[ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
]
}
]
}