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 要启用查询GeoJSON数据,您必须将该字段添加到2dsphere index. The following snippet creates an index on the location.geo field in the theaters collection using the createIndex() method:2dsphere索引中。以下代码片段使用createIndex()方法在影院集合中的location.geo字段上创建索引:
await 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. Until MongoDB, this was the only format compatible with geospatial queries, and are now referred to as "legacy coordinate pairs".x和y坐标表示地理空间查询。在MongoDB之前,这是唯一与地理空间查询兼容的格式,现在被称为“遗留坐标对”。
Legacy coordinate pairs use the following structure:传统坐标对使用以下结构:
<field> : [ x, y ]
The field contains 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. The following snippet creates an index on the coordinates field in the shipwrecks collection using the createIndex() method:2d索引。以下代码片段使用createIndex()方法在shipwrecks(沉船)集合中的坐标字段上创建索引:
await db.shipwrecks.createIndex({coordinates: "2d"});
See the MongoDB Server manual page on legacy coordinate pairs for more information.有关更多信息,请参阅MongoDB Server手册页上的传统坐标对。
Note
Spherical (球形(2dsphere) and flat (2d) indexes support some, but not all, of the same query operators. For a full list of operators and their index compatibility, consult the manual entry for geospatial queries.2dsphere)和平面(2d)索引支持一些(但不是全部)相同的查询运算符。有关运算符及其索引兼容性的完整列表,请参阅地理空间查询的手册条目。
Examples示例
The following examples use the MongoDB Atlas sample dataset. You can learn how to set up your own free-tier Atlas cluster and how to load the sample dataset in our get started guide.以下示例使用MongoDB Atlas示例数据集。您可以在入门指南中学习如何设置自己的免费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. To limit the results to a maximum distance in meters, use the $maxDistance option. $near运算符接受一组经纬度坐标,并返回从最近到最远排序的文档。要将结果限制为以米为单位的最大距离,请使用$maxDistance选项。For a complete list of options, see the reference documentation for 有关选项的完整列表,请参阅$near. The following example queries for theaters within 10,000 meters of [ -73.9667, 40.78 ].$near的参考文档。以下示例查询距离[-73.9667,40.78]10000米以内的影院。
// Find theaters within a certain proximity在一定距离内寻找剧院
async function proximity(theaters) {
// Define the query to find theaters near a specific location定义查询以查找特定位置附近的影院
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. The following example searches for movie theaters in the New England area:$geoWithin运算符选择具有指定形状内存在的地理空间数据的文档。以下示例搜索新英格兰地区的电影院:
// Find theaters within a specific geographic range查找特定地理范围内的影院
async function range(theaters) {
// Define the query to find theaters within a specified polygon定义查询以查找指定多边形内的影院
const query = {
"location.geo": {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[
[-72, 40], // Polygon coordinates defining the range定义范围的多边形坐标
[-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 Server手册中关于地理空间查询运算符的页面。