Search Geospatially地理空间搜索¶
On this page
Overview概述¶
You can query data based on geographical location using geospatial query operators. 可以使用地理空间查询运算符基于地理位置查询数据。You can format geospatial queries using one of the following coordinate systems:可以使用以下坐标系之一设置地理空间查询的格式:
This section contains examples of geospatial queries using different query operators that you can run against your Atlas sample dataset.本节包含使用不同查询运算符进行地理空间查询的示例,您可以对Atlas示例数据集运行这些查询运算符。
Coordinates on an Earth-like Sphere类地球球体上的坐标¶
For geospatial queries using longitude and latitude coordinates on an Earth-like sphere, use the GeoJSON query format. 对于使用类地球球体上的经度和纬度坐标的地理空间查询,请使用GeoJSON查询格式。While GeoJSON has multiple types, all GeoJSON data types use some form of the following structure:虽然GeoJSON有多种类型,但所有GeoJSON数据类型都使用以下某种形式的结构:
<field> : {
type: <GeoJSONtype>,
coordinates: [
[longitude_1, latitude_1],
...
[longitude_n, latitude_n]
]
}
The object type determines the number of coordinates. 对象类型确定坐标数。For instance, a 例如,一个Point
requires only one coordinate: a longitude and a latitude. Point
只需要一个坐标:经度和纬度。A Line
uses two coordinates: a longitude and a latitude for each end. Line
使用两个坐标:每一端的经度和纬度。A Polygon
consists of a list of coordinates in which the first and last coordinate are the same, effectively closing the polygon. Polygon
由第一个和最后一个坐标相同的坐标列表组成,有效地闭合多边形。To learn more about the GeoJSON shapes you can use in MongoDB, consult the GeoJSON manual entry.要了解有关可以在MongoDB中使用的GeoJSON形状的更多信息,请参阅GeoJSON手册条目。
To enable querying GeoJSON data, you must add the field to a 要启用查询GeoJSON数据,必须将该字段添加到2dsphere
index. 2dsphere
索引中。The following snippet creates an index on the 以下代码段使用location.geo
field in the theaters
collection using the createIndex()
method:createIndex()
方法在theaters
集合的location.geo
字段上创建索引:
db.theaters.createIndex({location.geo: "2dsphere"})
Coordinates on a 2D Plane二维平面上的坐标¶
You can also express geospatial queries using 还可以使用二维欧几里得平面中的x
and y
coordinates in a two-dimentional Euclidian plane. x
和y
坐标表示地理空间查询。Until MongoDB, this was the only format compatible with geospatial queries, and are now referred to as "legacy coordinate pairs".在MongoDB之前,这是唯一与地理空间查询兼容的格式,现在被称为“传统坐标对”。
Legacy coordinate pairs use the following structure:传统坐标对使用以下结构:
<field> : [ x, y ]
The field should contain an array of two values in which the first represents the 该字段应包含两个值的数组,其中第一个值表示x
axis value and the second represents the y
axis value.x
轴值,第二个值表示y
轴值。
To enable querying using legacy coordinate pairs, create a 要启用使用旧坐标对的查询,请在集合上的字段上创建2d
index on the field on the collection. 2d
索引。The following snippet creates an index on the 以下代码段使用coordinates
field in the shipwrecks
collection using the createIndex()
method:createIndex()
方法在shipwrecks
集合的coordinates
字段上创建索引:
db.shipwrecks({coordinates: "2d"})
See the MongoDB server manual page on legacy coordinate pairs for more information.有关更多信息,请参阅关于传统坐标对的MongoDB服务器手册页面。
Spherical (球面(2dsphere
) and flat (2d
) indexes support some, but not all, of the same query operators. 2dsphere
)和平面(2d
)索引支持一些但不是全部相同的查询运算符。For a full list of operators and their index compatibility, consult the manual entry for geospatial queries.有关运算符及其索引兼容性的完整列表,请参阅地理空间查询的手册条目。
Examples示例¶
The following examples use the MongoDB Atlas sample dataset. 以下示例使用MongoDB Atlas示例数据集。You can learn how to set up your own free-tier Atlas cluster and how to load the sample dataset in our quick start guide.您可以在我们的快速入门指南中学习如何设置自己的免费层Atlas集群以及如何加载示例数据集。
The examples use the 这些示例使用样本数据集中theaters
collection in the sample_mflix
database from the sample dataset. sample_mflix
数据库中的theaters
集合。The theaters
collection contains a 2dsphere
index on the location.geo
field.theaters
集合包含location.geo
字段上的2dsphere
索引。
Query by Proximity¶
The $near operator accepts a set of longitude-latitude coordinates and returns documents ordered from nearest to farthest. $near
运算符接受一组经纬度坐标,并返回从最近到最远排序的文档。To limit the results to a maximum distance in meters, use the 要将结果限制为以米为单位的最大距离,请使用$maxDistance
option. $maxDistance
选项。For a complete list of options, see the reference documentation for 有关选项的完整列表,请参阅$near
. $near
的参考文档。The following example queries for theaters within 以下示例查询离10,000
meters of [ -73.9667, 40.78 ]
.[ -73.9667, 40.78 ]
10000米范围内的影院。
const query = {
"location.geo": {
$near: {
$geometry: { type: "Point", coordinates: [-73.9667, 40.78] },
$maxDistance: 10000,
},
},
};
// find documents based on our query
const cursor = theaters.find(query);
Query Within a Range范围内的查询¶
The $geoWithin operator selects documents with geospatial data that exist within a specified shape. $geoWithin操作符用于选择包含存在于指定形状中的地理空间数据的文档。The following example searches for movie theaters in the New England area:以下示例搜索新英格兰地区的电影院:
const query = {
"location.geo": {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[
[-72, 40],
[-74, 41],
[-72, 39],
[-72, 40],
],
],
},
},
},
};
// find documents based on our query
const cursor = theaters.find(query);
See the MongoDB server manual page on geospatial query operators for more information on the operators you can use in your query.有关可在查询中使用的运算符的更多信息,请参阅MongoDB服务器手册中有关地理空间查询运算符的页面。