不安分的黑娃
踏踏实实,坚持学习,慢慢就懂了~

MongoDB 支持地理空间数据存储

官方文档

https://docs.mongodb.com/manual/geospatial-queries/

MongoDB 支持对于地理空间数据的查询操作.

空间地理数据

MongoDB 中使用 GeoJSON对象 或 坐标对 描述空间地理数据.MongoDB使用 WGS84 参考系进行地理空间数据查询.

GeoJSON 对象

GeoJSON 对象格式

<field>: { type: <GeoJSON type> , coordinates: <coordinates> }

GeoJSON 对象有两个filed,分别是 type 和 coordinates.其中,

  • type 指明是哪种空间地理数据类型
  • coordinates: 是描述 Geo对象的坐标数组,经度在前(经度取值范围 -180到 180),纬度在后(纬度取值范围是-90到90)

rfc7946

rfc7946 标准描述了 GeoJSON对象如何描述空间元素.
https://datatracker.ietf.org/doc/html/rfc7946#appendix-A

Point

声明一个点:

{ type: "Point", coordinates: [ 40, 5 ] }

LineString

声明一个线:

{ type: "LineString", coordinates: [ [ 40, 5 ], [ 41, 6 ] ] }

Polygon

声明一个多边形(单个环):

{
  type: "Polygon",
  coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0  ] ] ]
}

声明多个多边形(多个嵌套):

  • 第一个描述的必须是外部的环
  • 外部环不能自我相交
  • 任一内部环必须完全被外部环包含
  • 内部每个环不能相交或遮盖
{
  type : "Polygon",
  coordinates : [
     [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ],
     [ [ 2 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] , [ 2 , 2 ] ]
  ]
}

image

MultiPoint

版本 2后, 2dsphere 索引开始支持 MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection.

声明多个点:

{
  type: "MultiPoint",
  coordinates: [
     [ -73.9580, 40.8003 ],
     [ -73.9498, 40.7968 ],
     [ -73.9737, 40.7648 ],
     [ -73.9814, 40.7681 ]
  ]
}

MultiLineString

声明 多个线段

{
  type: "MultiLineString",
  coordinates: [
     [ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
     [ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
     [ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
     [ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
  ]
}

MultiPolygon

声明多个多边形:

{
  type: "MultiPolygon",
  coordinates: [
     [ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.9814, 40.7681 ], [ -73.958, 40.8003 ] ] ],
     [ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.958, 40.8003 ] ] ]
  ]
}

GeometryCollection

{
  type: "GeometryCollection",
  geometries: [
     {
       type: "MultiPoint",
       coordinates: [
          [ -73.9580, 40.8003 ],
          [ -73.9498, 40.7968 ],
          [ -73.9737, 40.7648 ],
          [ -73.9814, 40.7681 ]
       ]
     },
     {
       type: "MultiLineString",
       coordinates: [
          [ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
          [ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
          [ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
          [ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
       ]
     }
  ]
}

地理空间索引

MongoDB 提供了以下空间索引类型支持空间数据计算.

分片键不能使用地理空间索引!
分片的Collection支持的操作:

  • $geoNear
  • $near and $nearSphere query operators (starting in MongoDB 4.0)
    地理空间索引不能覆盖查询.

2dsphere

2dsphere索引支持 类地球形几何图形计算.
创建 2dsphere 索引:

db.collection.createIndex( { <location field> : "2dsphere" } )

2d索引

2d 索引支持对二维平面内的几何图形的查询.
创建 2d 索引:

db.collection.createIndex( { <location field> : "2d" } )

地理空间查询

https://docs.mongodb.com/manual/geospatial-queries/
MongoDB 提供了以下地理空间查询操作:

  • $geoIntersects
  • $geoWithin
  • $near
  • $nearSphere
    管道聚合:
  • $geoNear
posted on 2022-02-14 17:04  不安分的黑娃  阅读(564)  评论(0编辑  收藏  举报