Geospatial Queries地理空间查询
On this page本页内容
MongoDB supports query operations on geospatial data. This section introduces MongoDB's geospatial features.MongoDB支持对地理空间数据的查询操作。本节介绍MongoDB的地理空间功能。
Geospatial Data空间数据
In MongoDB, you can store geospatial data as GeoJSON objects or as legacy coordinate pairs.在MongoDB中,您可以将地理空间数据存储为GeoJSON对象或遗留坐标对。
GeoJSON ObjectsGeoJSON对象
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数据,请使用带有以下内容的嵌入式文档:
a field named指定GeoJSON对象类型的名为type
that specifies the GeoJSON object type andtype
的字段,以及a field named一个名为coordinates
that specifies the object's coordinates.coordinates
的字段,用于指定对象的坐标。ImportantIf specifying latitude and longitude coordinates, list the longitude first, and then latitude.如果指定纬度和经度坐标,请先列出经度,然后列出纬度。Valid longitude values are between有效的经度值介于-180
and180
, both inclusive.-180
和180
之间(包括-180
和180
)。Valid latitude values are between有效的纬度值介于-90
and90
, both inclusive.-90
和90
之间(包括-90
和90
)。
<field>: { type: <GeoJSON type> , coordinates: <coordinates> }
For example, to specify a GeoJSON Point:例如,要指定GeoJSON点:
location: {
type: "Point",
coordinates: [-73.856077, 40.848447]
}
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对象进行地理空间查询。
Legacy Coordinate Pairs传统坐标对
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 by using a 如果手动将数据转换为GeoJSON Point类型,MongoDB通过使用2dsphere
index if you manually convert the data to the GeoJSON Point type.2dsphere
索引来支持在遗留坐标对上进行球面计算。
To specify data as legacy coordinate pairs, you can use either an array (preferred) or an embedded document.要将数据指定为传统坐标对,可以使用数组(首选)或嵌入文档。
Specify via an array (Preferred):通过数组指定(首选):-
<field>: [ <x>, <y> ]
If specifying latitude and longitude coordinates, list the longitude first and then latitude; i.e.如果指定纬度和经度坐标,请先列出经度,然后列出纬度;即。<field>: [<longitude>, <latitude> ]
Valid longitude values are between有效的经度值介于-180
and180
, both inclusive.-180
和180
之间(包括-180
和180
)。Valid latitude values are between有效的纬度值介于-90
and90
, both inclusive.-90
和90
之间(包括-90
和90
)。
Specify via an embedded document:通过嵌入文档指定:-
<field>: { <field1>: <x>, <field2>: <y> }
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.如果指定纬度和经度坐标,则无论字段名称如何,第一个字段都必须包含经度值,第二个字段必须包含纬度值;即。<field>: { <field1>: <longitude>, <field2>: <latitude> }
Valid longitude values are between有效的经度值介于-180
and180
, both inclusive.-180
和180
之间(包括-180
和180
)。Valid latitude values are between有效的纬度值介于-90
and90
, 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.若要指定传统坐标对,数组优先于嵌入文档,因为某些语言不能保证关联地图排序。
Geospatial Indexes地理空间索引
MongoDB provides the following geospatial index types to support geospatial queries. MongoDB提供以下地理空间索引类型来支持地理空间查询。For more information on geospatial indexes, see Geospatial Indexes.有关地理空间索引的详细信息,请参阅地理空间索引。
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"
作为索引类型:
db.collection.createIndex( { <location field> : "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"
指定为索引类型:
db.collection.createIndex( { <location field> : "2d" } )
where the 其中<location field>
is a field whose value is a legacy coordinates pair.<location field>
是一个字段,其值为遗留坐标对。
For more information on the 有关2d
index, see 2d Indexes.2d
索引的详细信息,请参阅2d
索引。
Geospatial Queries地理空间查询
Using a 使用2d
index for queries on spherical data can return incorrect results or an error. 2d
索引查询球形数据可能会返回不正确的结果或错误。For example, 例如,2d
indexes don't support spherical queries that wrap around the poles.2d
索引不支持环绕极点的球形查询。
Geospatial Query Operators地理空间查询运算符
MongoDB provides the following geospatial query operators:MongoDB提供以下地理空间查询运算符:
$geoIntersects | 2dsphere index supports $geoIntersects .2dsphere 索引支持$geoIntersects 。 |
$geoWithin | 2dsphere and 2d indexes support $geoWithin .2dsphere 和2d 索引支持$geoWithin 。 |
$near | 2dsphere and 2d indexes support $near .2dsphere 和2d 索引支持$near 。 |
$nearSphere | 2dsphere and 2d indexes support $nearSphere .2dsphere 和2d 索引支持$nearSphere 。 |
For more details, including examples, see the individual reference page.有关更多详细信息(包括示例),请参阅个人参考页。
Geospatial Aggregation Stage地理空间聚合阶段
MongoDB provides the following geospatial aggregation pipeline stage:MongoDB提供了以下地理空间聚合管道阶段:
Stage | |
---|---|
$geoNear | $match , $sort , and $limit for geospatial data. $match 、$sort 和$limit 功能。$geoNear |
For more details, including examples, see 有关更多详细信息(包括示例),请参阅$geoNear
reference page.$geoNear
参考页。
Geospatial Models地理空间模型
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).索引只支持球形查询(即解释球面上几何图形的查询)。
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 (GeoJSON2dsphere 索引) | $nearSphere operator, which provides the same functionality when used with GeoJSON and a 2dsphere index.$nearSphere 运算符,它在与GeoJSON和2dsphere索引一起使用时提供相同的功能。 | |
$near 2d 索引) | ||
$nearSphere (GeoJSON point, 2dsphere index) | $near operation that uses GeoJSON point and a 2dsphere index.$near 点和2dsphere索引的$near 操作相同的功能。$nearSphere which explicitly specifies the spherical queries in the name rather than $near operator. $nearSphere ,它在名称中显式指定球形查询,而不是使用$near 运算符。 | |
$nearSphere (legacy coordinates, 2d index) | ||
$geoWithin : { $geometry : ... } | ||
$geoWithin : { $box : ... } | ||
$geoWithin : { $polygon : ... } | ||
$geoWithin : { $center : ... } | ||
$geoWithin : { $centerSphere : ... } | ||
$geoIntersects | ||
$geoNear 2dsphere 索引) | ||
$geoNear 2d 索引) |
Examples实例
Create a collection 使用以下文档创建集合places
with the following documents:places
:
db.places.insertMany( [
{
name: "Central Park",
location: { type: "Point", coordinates: [ -73.97, 40.77 ] },
category: "Parks"
},
{
name: "Sara D. Roosevelt Park",
location: { type: "Point", coordinates: [ -73.9928, 40.7193 ] },
category: "Parks"
},
{
name: "Polo Grounds",
location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
category: "Stadiums"
}
] )
The following operation creates a 以下操作将在2dsphere
index on the location
field:location
字段上创建2dsphere
索引:
db.places.createIndex( { location: "2dsphere" } )
The 上面的places
collection above has a 2dsphere
index. places
集合有一个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米的文档,按从最近到最远的顺序排序:
db.places.find(
{
location:
{ $near:
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$minDistance: 1000,
$maxDistance: 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点:
db.places.aggregate( [
{
$geoNear: {
near: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
spherical: true,
query: { category: "Parks" },
distanceField: "calcDistance"
}
}
] )