On this page本页内容
MongoDB supports query operations on geospatial data. MongoDB支持对地理空间数据的查询操作。This section introduces MongoDB’s geospatial features.本节介绍MongoDB的地理空间功能。
In MongoDB, you can store geospatial data as GeoJSON objects or as legacy coordinate pairs.在MongoDB中,可以将地理空间数据存储为GeoJSON对象或传统坐标对。
To calculate geometry over an Earth-like sphere, store your location data as GeoJSON objects.要计算类地球球体上的几何体,请将位置数据存储为GeoJSON对象。
To specify GeoJSON data, use an embedded document with:要指定GeoJSON数据,请使用带有以下内容的嵌入式文档:
type
that specifies the GeoJSON object type andtype
的字段,指定GeoJSON对象类型,以及coordinates
that specifies the object’s coordinates.coordinates
的字段,用于指定对象的坐标。
If specifying latitude and longitude coordinates, list the longitude first and then latitude:如果指定纬度和经度坐标,请先列出经度,然后列出纬度:
-180
and 180
, both inclusive.-180
和180
之间(包括-180
和180
)。-90
and 90
, both inclusive.-90
和90
之间(包括-90
和90
)。For example, to specify a GeoJSON Point:例如,要指定GeoJSON点:
For a list of the GeoJSON objects supported in MongoDB as well as examples, see GeoJSON objects.有关MongoDB中支持的GeoJSON对象的列表以及示例,请参阅GeoJSON对象。
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对象进行地理空间查询。
To calculate distances on a Euclidean plane, store your location data as legacy coordinate pairs and use a 2d index. 要计算欧几里德平面上的距离,请将位置数据存储为传统坐标对,并使用2d索引。MongoDB supports spherical surface calculations on legacy coordinate pairs via a 2dsphere index by converting the data to the GeoJSON Point type.MongoDB通过将数据转换为GeoJSON点类型,通过2dsphere索引支持传统坐标对的球面计算。
To specify data as legacy coordinate pairs, you can use either an array (preferred) or an embedded document.要将数据指定为传统坐标对,可以使用数组(首选)或嵌入式文档。
If specifying latitude and longitude coordinates, list the longitude first and then latitude; i.e.如果指定纬度和经度坐标,请先列出经度,然后列出纬度;即
-180
and 180
, both inclusive.-180
和180
之间,两者都包括在内。-90
and 90
, both inclusive.-90
和90
之间(包括-90
和90
)。If specifying latitude and longitude coordinates, the first field, regardless of the field name, must contains the longitude value and the second field, the latitude value ; i.e.如果指定纬度和经度坐标,无论字段名称如何,第一个字段必须包含经度值,第二个字段必须包含纬度值;即
-180
and 180
, both inclusive.-180
和180
之间(包括-180
和180
)。-90
and 90
, both inclusive.-90
和90
之间(包括-90
和90
)。To specify legacy coordinate pairs, arrays are preferred over an embedded document as some languages do not guarantee associative map ordering.要指定传统坐标对,数组优先于嵌入式文档,因为某些语言不能保证关联地图顺序。
MongoDB provides the following geospatial index types to support the geospatial queries.MongoDB提供以下地理空间索引类型以支持地理空间查询。
2dsphere
¶2dsphere indexes support queries that calculate geometries on an earth-like sphere.2dsphere索引支持在类地球球体上计算几何图形的查询。
To create a 要创建2dsphere
index, use the db.collection.createIndex()
method and specify the string literal "2dsphere"
as the index type:2dsphere
索引,请使用db.collection.createIndex()
方法,并将字符串文本"2dsphere"
指定为索引类型:
where the 其中,<location field>
is a field whose value is either a GeoJSON object or a legacy coordinates pair.<location field>
是一个值为GeoJSON对象或遗留坐标对的字段。
For more information on the 有关2dsphere
index, see 2dsphere Indexes.2dsphere
索引的更多信息,请参阅2dsphere索引。
2d
¶2d indexes support queries that calculate geometries on a two-dimensional plane. 2d索引支持在二维平面上计算几何图形的查询。Although the index can support 虽然该索引可以支持在球体上计算的$nearSphere
queries that calculate on a sphere, if possible, use the 2dsphere index for spherical queries.$nearSphere
查询,但如果可能,请使用2dsphere索引进行球体查询。
To create a 要创建2d
index, use the db.collection.createIndex()
method, specifying the location field as the key and the string literal "2d"
as the index type:2d
索引,请使用db.collection.createIndex()
方法,将位置字段指定为键,将字符串文字"2d"
指定为索引类型:
where the 其中,<location field>
is a field whose value is a legacy coordinates pair.<location field>
是一个值为传统坐标对的字段。
For more information on the 有关二维索引的详细信息,请参阅2d索引。2d
index, see 2d Indexes.
You cannot use a geospatial index as a shard key when sharding a collection. 切分集合时,不能将地理空间索引用作切分键。However, you can create a geospatial index on a sharded collection by using a different field as the shard key.但是,可以使用其他字段作为分片键,在分片集合上创建地理空间索引。
The following geospatial operations are supported on sharded collections:分片集合支持以下地理空间操作:
$geoNear
$near
and $nearSphere
query operators (starting in MongoDB 4.0)$near
和$nearSphere
查询运算符(从MongoDB 4.0开始)Starting in MongoDB 4.0, 从MongoDB 4.0开始,分片集合支持$near
and $nearSphere
queries are supported for sharded collections.$near
和$nearSphere
查询。
In earlier MongoDB versions, 在早期的MongoDB版本中,分片集合不支持$near
and $nearSphere
queries are not supported for sharded collections; instead, for sharded clusters, you must use the $geoNear
aggregation stage or the geoNear
command (available in MongoDB 4.0 and earlier).$near
和$nearSphere
查询;相反,对于分片集群,必须使用$geoNear
聚合阶段或geoNear
命令(在MongoDB 4.0及更早版本中提供)。
You can also query for geospatial data for a sharded cluster using 还可以使用$geoWithin
and $geoIntersect
.$geoWithin
和$geoIntersect
查询分片集群的地理空间数据。
Geospatial indexes cannot cover a query.地理空间索引不能覆盖查询。
Note
For spherical queries, use the 对于球形查询,请使用2dsphere
index result.2dsphere
索引结果。
The use of 对球形查询使用2d
index for spherical queries may lead to incorrect results, such as the use of the 2d
index for spherical queries that wrap around the poles.2d
索引可能会导致错误的结果,例如对环绕极点的球形查询使用2d
索引。
MongoDB provides the following geospatial query operators:MongoDB提供以下地理空间查询运算符:
$geoIntersects |
$geoIntersects .$geoIntersects 。 |
$geoWithin |
$geoWithin .$geoWithin 。 |
$near |
$near .$near 。 |
$nearSphere |
$nearSphere . |
For more details, including examples, see the individual reference page.有关更多详细信息(包括示例),请参阅个人参考页。
MongoDB provides the following geospatial aggregation pipeline stage:MongoDB提供以下地理空间聚合管道阶段:
Stage | |
---|---|
$geoNear |
|
For more details, including examples, see 有关更多详细信息(包括示例),请参阅$geoNear
reference page.$geoNear
参考页。
MongoDB geospatial queries can interpret geometry on a flat surface or a sphere.MongoDB地理空间查询可以解释平面或球体上的几何图形。
2dsphere
indexes support only spherical queries (i.e. queries that interpret geometries on a spherical surface).2dsphere
索引只支持球形查询(即解释球面上几何图形的查询)。
2d
indexes support flat queries (i.e. queries that interpret geometries on a flat surface) and some spherical queries. 2d
索引支持平面查询(即解释平面上几何图形的查询)和一些球形查询。While 虽然2d
indexes support some spherical queries, the use of 2d
indexes for these spherical queries can result in error. 2d
索引支持一些球形查询,但对这些球形查询使用2d
索引可能会导致错误。If possible, use 如果可能,使用2dsphere
indexes for spherical queries.2dsphere
索引进行球形查询。
The following table lists the geospatial query operators, supported query, used by each geospatial operations:下表列出了每个地理空间操作所使用的地理空间查询运算符(支持的查询):
$near (GeoJSON |
Spherical | $nearSphere operator, which provides the same functionality when used with GeoJSON and a 2dsphere index.$nearSphere 运算符,它在与GeoJSON和2dsphere索引一起使用时提供相同的功能。 |
$near (legacy coordinates, 2d index) |
Flat | |
$nearSphere (GeoJSON point, 2dsphere index) |
Spherical |
|
$nearSphere (legacy coordinates, 2d index) |
Spherical | |
$geoWithin : { $geometry : … } |
Spherical | |
$geoWithin : { $box : … } |
Flat | |
$geoWithin : { $polygon : … } |
Flat | |
$geoWithin : { $center : … } |
Flat | |
$geoWithin : { $centerSphere : … } |
Spherical | |
$geoIntersects |
Spherical | |
$geoNear aggregation stage (2dsphere index)$geoNear 聚合阶段(2dsphere索引) |
Spherical | |
$geoNear |
Flat |
Create a collection 使用以下文档创建集合places
with the following documents:places
:
The following operation creates a 以下操作将在2dsphere
index on the location
field:location
字段上创建2dsphere
索引:
The following query uses the 以下查询使用$near
operator to return documents that are at least 1000 meters from and at most 5000 meters from the specified GeoJSON point, sorted in order from nearest to farthest:$near
运算符返回距离指定GeoJSON点至少1000米、最多5000米的文档,按从最近到最远的顺序排序:
The following operation uses the 以下操作使用geoNear
aggregation operation to return documents that match the query filter { category: "Parks" }
, sorted in order of nearest to farthest to the specified GeoJSON point:geoNear
聚合操作返回与查询筛选器{ category: "Parks" }
匹配的文档,并按距离指定GeoJSON点最近到最远的顺序进行排序: