Docs 菜单
Docs 主页
/ / /
Pymongo 驱动程序
/

地理空间查询

在此页面上

  • Overview
  • 地理空间数据格式
  • GeoJSON
  • 传统坐标对
  • 地理空间索引
  • 查询运算符
  • 示例
  • 按距离查询
  • 按多边形查询
  • 其他资源

在本指南中,您可以学习;了解如何使用地理空间数据、数据格式、索引和查询。

地理空间数据表示地球表面的位置。

地理空间数据的示例包括:

  • 电影院位置

  • 国家/地区边界

  • 自行车骑行路线

  • 纽约市的狗狗运动区

  • 图表上的点

MongoDB 中的所有地理空间数据都使用以下格式之一存储:

  • GeoJSON,一种表示类似地球的球体上的地理空间数据的格式

  • 传统坐标对,一种在欧几里得平面上表示地理空间数据的格式

使用 GeoJSON 来存储表示类地球体上地理空间信息的数据。GeoJSON 由一个或多个位置和一个类型组成。

位置表示单个位置,并在代码中作为包含以下值的大量存在:

  • 第一个位置的经度(必需)

  • 第二个位置的纬度(必需)

  • 第三个位置的海拔高度(选填)

以下是 MongoDB 总部在纽约州纽约市的位置。

[-73.986805, 40.7620853]

重要

经度然后纬度

GeoJSON以经度在前、纬度在后的方式对坐标进行排序。 请务必检查您正在使用的任何其他工具使用的格式,因为许多流行的工具(例如 OpenStreetMap 和 Google Maps)都是以纬度在前、经度在后的方式列出坐标。

GeoJSON对象的类型决定了它所表示的几何形状。 几何形状由多个位置组成。

下面是一些常见的 GeoJSON 类型以及如何使用位置指定这些类型:

  • Point:单个位置。下面的 Point 表示 MongoDB 总部的位置:

    {
    "type": "Point",
    "coordinates": [-73.856077, 40.848447]
    }
  • LineString:由两个或多个位置组成的大量,构成一系列线段。 LineString可以表示路径、路线、边界或任何其他线性地理空间数据。 以下LineString代表中国长城的一段:

    {
    "type": "LineString",
    "coordinates":
    [[116.572, 40.430],
    [116.570, 40.434],
    [116.567, 40.436],
    [116.566, 40.441]]
    }
  • Polygon:位置大量,其中第一个和最后一个位置相同,并包含一些空格。 以下Polygon大致代表梵蒂冈城内的土地:

    {
    "type": "Polygon",
    "coordinates":
    [[[12.446086, 41.901977],
    [12.457952, 41.901559],
    [12.455375, 41.907351],
    [12.449863, 41.905186],
    [12.446086, 41.901977]]]
    }

要学习;了解有关可在MongoDB中使用的GeoJSON类型的详情,请参阅 GeoJSON手册条目。

有关GeoJSON格式的更多信息,请参阅官方 IETF 规范。

使用legacy coordinate pairs存储表示二维平面上的地理空间信息的数据。

传统坐标对由包含两个值的大量表示,其中第一个值表示 x 轴值,第二个值表示 y 轴值。

有关旧版坐标对的更多信息,请参阅 MongoDB 服务器手册中有关旧版坐标对的页面

要启用对地理空间数据的查询,必须创建与数据格式相对应的索引。以下索引类型支持地理空间查询:

  • 2dsphere,用于GeoJSON数据

  • 2d,用于legacy coordinate pairs

要详细学习;了解如何创建地理空间索引,请参阅 索引指南的地理空间索引部分。

要使用 find操作符查询地理空间数据,请使用以下查询操作符之一:

  • $near

  • $geoWithin

  • $nearSphere

  • $geoIntersects需要2 dsphere索引

使用 $near 操作符时,可以指定以下距离操作符:

  • $minDistance

  • $maxDistance

使用 $geoWithin 操作符时,可以指定以下形状操作符:

  • $box

  • $polygon

  • $center

  • $centerSphere

要使用 aggregate操作符查询地理空间数据,您必须使用 $geoNear管道阶段。

有关地理空间查询操作符的更多信息,请参阅服务器手册中的地理空间查询操作符

以下示例使用MongoDB Atlas示例数据集。要获取此示例数据集,请参阅Atlas示例数据集。要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅PyMongo入门。

示例使用了样本数据集 sample_mflix 数据库中的 theaters 集合。theaters 集合包含 location.geo 字段上的 2dsphere 索引。

以下示例查询具有 location.geo字段值在距离纽约市MongoDB总部 1000 米范围内的文档。它按从最近到最远的顺序返回文档。选择 SynchronousAsynchronous标签页以查看相应的代码:

# set query with point at MongoDB headquarters and a maxDistance of 1000 meters
query = {
"location.geo": {
"$near": {
"$geometry": {
# Search around this location
"type": "Point",
"coordinates": [-73.986805, 40.7620853]
},
"$maxDistance": 1000 # Distance in meters (1 km)
}
}
}
# fetches the _id and theaterId fields
projection = { "theaterId": 1 }
nearby_places = collection.find(query, projection)
for i in nearby_places:
print(i)
{ "_id" : ObjectId("59a47287cfa9a3a73e51e8e2"), "theaterId" : 1908 }
{ "_id" : ObjectId("59a47286cfa9a3a73e51e838"), "theaterId" : 1448 }
# set query with point at MongoDB headquarters and a maxDistance of 1000 meters
query = {
"location.geo": {
"$near": {
"$geometry": {
# Search around this location
"type": "Point",
"coordinates": [-73.986805, 40.7620853]
},
"$maxDistance": 1000 # Distance in meters (1 km)
}
}
}
# fetches the _id and theaterId fields
projection = { "theaterId": 1 }
nearby_places = collection.find(query, projection)
async for i in nearby_places:
print(i)
{ "_id" : ObjectId("59a47287cfa9a3a73e51e8e2"), "theaterId" : 1908 }
{ "_id" : ObjectId("59a47286cfa9a3a73e51e838"), "theaterId" : 1448 }

以下示例查询位于曼哈顿范围内且字段值为 location.geo 的文档。选择SynchronousAsynchronous标签页以查看相应的代码:

# Polygon representation of Manhattan
query = {
"location.geo": {
"$geoWithin": {
"$geometry": {
# Search around this location
"type": "Polygon",
"coordinates":
[[[-73.925492, 40.877410],
[-73.910372, 40.872366],
[-73.935127, 40.834020],
[-73.929049, 40.798569],
[-73.976485, 40.711432],
[-74.015747, 40.701229],
[-74.018859, 40.708367],
[-74.008007, 40.754307],
[-73.925492, 40.877410]]]
}
}
}
}
# fetches the _id and theaterId fields
projection = { "theaterId": 1 }
nearby_places = collection.find(query, projection)
for i in nearby_places:
print(i)
{ "_id" : ObjectId("59a47287cfa9a3a73e51e8e2"), "theaterId" : 1908 }
{ "_id" : ObjectId("59a47287cfa9a3a73e51eccb"), "theaterId" : 835 }
{ "_id" : ObjectId("59a47286cfa9a3a73e51e838"), "theaterId" : 1448 }
{ "_id" : ObjectId("59a47286cfa9a3a73e51e744"), "theaterId" : 1028 }
{ "_id" : ObjectId("59a47287cfa9a3a73e51ebe1"), "theaterId" : 609 }
{ "_id" : ObjectId("59a47287cfa9a3a73e51e8ed"), "theaterId" : 1906 }
{ "_id" : ObjectId("59a47287cfa9a3a73e51e87d"), "theaterId" : 1531 }
{ "_id" : ObjectId("59a47287cfa9a3a73e51eb63"), "theaterId" : 482 }
# Polygon representation of Manhattan
query = {
"location.geo": {
"$geoWithin": {
"$geometry": {
# Search around this location
"type": "Polygon",
"coordinates":
[[[-73.925492, 40.877410],
[-73.910372, 40.872366],
[-73.935127, 40.834020],
[-73.929049, 40.798569],
[-73.976485, 40.711432],
[-74.015747, 40.701229],
[-74.018859, 40.708367],
[-74.008007, 40.754307],
[-73.925492, 40.877410]]]
}
}
}
}
# fetches the _id and theaterId fields
projection = { "theaterId": 1 }
nearby_places = collection.find(query, projection)
async for i in nearby_places:
print(i)
{ "_id" : ObjectId("59a47287cfa9a3a73e51e8e2"), "theaterId" : 1908 }
{ "_id" : ObjectId("59a47287cfa9a3a73e51eccb"), "theaterId" : 835 }
{ "_id" : ObjectId("59a47286cfa9a3a73e51e838"), "theaterId" : 1448 }
{ "_id" : ObjectId("59a47286cfa9a3a73e51e744"), "theaterId" : 1028 }
{ "_id" : ObjectId("59a47287cfa9a3a73e51ebe1"), "theaterId" : 609 }
{ "_id" : ObjectId("59a47287cfa9a3a73e51e8ed"), "theaterId" : 1906 }
{ "_id" : ObjectId("59a47287cfa9a3a73e51e87d"), "theaterId" : 1531 }
{ "_id" : ObjectId("59a47287cfa9a3a73e51eb63"), "theaterId" : 482 }

后退

配置增删改查操作