Docs HomeNode.js

Search Geospatially搜索地理空间

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. While GeoJSON has multiple types, all GeoJSON data types use some form of the following structure:对于在类地球球体上使用经度和纬度坐标的地理空间查询,请使用GeoJSON查询格式。虽然GeoJSON有多种类型,但所有GeoJSON数据类型都使用以下结构形式:

<field> : {
type: <GeoJSON type>,
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 2dsphere index. 要启用查询GeoJSON数据,必须将字段添加到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-dimensional Euclidean plane. 也可以使用二维欧几里得平面中的xy坐标来表达地理空间查询。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服务器手册中关于遗留坐标对的页面。

Note

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. The theaters collection contains a 2dsphere index on the location.geo field.这些示例使用范例数据集中sample_mflix数据库中的theaters集合。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服务器手册页