Docs HomeMongoDB Manual

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指定GeoJSON对象类型的名为type的字段,以及
  • a field named coordinates that specifies the object's 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)。
<field>: { type: <GeoJSON type> , coordinates: <coordinates> }

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

Point

The following example specifies a GeoJSON Point:以下示例指定GeoJSONPoint

{ 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. 这些LinearRings是闭合的LineStringsClosed LineStrings have at least four coordinate pairs and specify the same position as the first and last coordinates.闭合的LineStrings至少有四个坐标对,并指定与第一个和最后一个坐标相同的位置。

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:以下示例指定了一个GeoJSONPolygon,该多边形有一个外部环,没有内部环(或孔)。第一个和最后一个坐标必须匹配才能闭合多边形:

{
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 MultiPoint嵌入文档对点列表进行编码。

{
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 MultiPolygon

{
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类型GeometryCollection的坐标:

{
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 ] ]
]
}
]
}