zoukankan      html  css  js  c++  java
  • mongodb-地理坐标存储查询

    mongodb可支持空间地理搜索: 

    查询器

    $geoWithin       Selects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support $geoWithin. replaces $within which is deprecated.
    $geoIntersects     Selects geometries that intersect with a GeoJSON geometry. The 2dsphere index supports $geoIntersects.
    $near          Returns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near.
    $nearSphere       Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere.

    查询参数: 

    $geometry Specifies a geometry in GeoJSON format to geospatial query operators.
    $minDistance Specifies a minimum distance to limit the results of $near and $nearSphere queries. For use with 2dsphere index only.
    $maxDistance Specifies a maximum distance to limit the results of $near and $nearSphere queries. The 2dsphereand 2d indexes support $maxDistance.
    $center Specifies a circle using legacy coordinate pairs to $geoWithin queries when using planar geometry. The 2d index supports $center.
    $centerSphere Specifies a circle using either legacy coordinate pairs or GeoJSON format for $geoWithin queries when using spherical geometry. The 2dsphere and 2d indexes support $centerSphere.
    $box Specifies a rectangular box using legacy coordinate pairs for $geoWithin queries. The 2d index supports $box.
    $polygon Specifies a polygon to using legacy coordinate pairs for $geoWithin queries. The 2d index supports$center.
    $uniqueDocs Deprecated. Modifies a $geoWithin and $near queries to ensure that even if a document matches the query multiple times, the query returns the document once.

    1, geoWithIn查询, 替代以前的wihin查询, 查询多边形范围内的点

    db.places.find(
       {
         loc: {
           $geoWithin: {
              $geometry: {
                 type : "Polygon" ,
                 coordinates: [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ]
              }
           }
         }
       }
    )

    对于大于单个半球的查询, 需要加入crs

    db.places.find(
       {
         loc: {
           $geoWithin: {
              $geometry: {
                 type : "Polygon" ,
                 coordinates: [
                   [
                     [ -100, 60 ], [ -100, 0 ], [ -100, -60 ], [ 100, -60 ], [ 100, 60 ], [ -100, 60 ]
                   ]
                 ],
                 crs: {
                    type: "name",
                    properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
                 }
              }
           }
         }
       }
    )

    2, geoIntersects, 图形查询, 交集

    db.places.find(
       {
         loc: {
           $geoIntersects: {
              $geometry: {
                 type: "Polygon" ,
                 coordinates: [
                   [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ]
                 ]
              }
           }
         }
       }
    )

    查询大于半个半球的

    db.places.find(
       {
         loc: {
           $geoIntersects: {
              $geometry: {
                 type : "Polygon",
                 coordinates: [
                   [
                     [ -100, 60 ], [ -100, 0 ], [ -100, -60 ], [ 100, -60 ], [ 100, 60 ], [ -100, 60 ]
                   ]
                 ],
                 crs: {
                    type: "name",
                    properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
                 }
              }
           }
         }
       }
    )

    3, $near, 由近道原返回文档的点, 经纬度罗列方式为 [ lng, lat ]

    需要创建空间索引

    2dsphere index if specifying a GeoJSON point,
    2d index if specifying a point using legacy coordinates.
    db.places.ensureIndex( { loc : "2d" } )     //应该是固定格式  
    db.places.find(
       {
         location:
           { $near :
              {
                $geometry: { type: "Point",  coordinates: [ -73.9667, 40.78 ] },
                $minDistance: 1000,
                $maxDistance: 5000
              }
           }
       }
    )

    使用传统坐标查询: 

    db.legacy2d.find(
       { location : { $near : [ -73.9667, 40.78 ], $maxDistance: 0.10 } }
    )

    4, $nearSphere, 空间距离查询

    同样需要建立空间索引

    2dsphere index for location data defined as GeoJSON points
    2d index for location data defined as legacy coordinate pairs. To use a 2d index on GeoJSON points, create the index on the coordinates field of the GeoJSON object.
    db.places.find(
       {
         location: {
            $nearSphere: {
               $geometry: {
                  type : "Point",
                  coordinates : [ -73.9667, 40.78 ]
               },
               $minDistance: 1000,
               $maxDistance: 5000
            }
         }
       }
    )

    最大距离内查询: 

    db.places.find( {
       loc: { $near: [ -74 , 40 ],  $maxDistance: 10 }
    } )

    5, $center查询, 圆形查询

    db.places.find(
       { loc: { $geoWithin: { $center: [ [-74, 40.74], 10 ] } } }
    )

    6, $centerSphere 查询, 球星查询

    db.places.find( {
      loc: { $geoWithin: { $centerSphere: [ [ -88, 30 ], 10/3963.2 ] } }
    } )

    7, $box查询, 先精度后纬度, first lower then upper

    db.places.find( {
       loc: { $geoWithin: { $box:  [ [ 0, 0 ], [ 100, 100 ] ] } }
    } )

    8, $polygon, 多边形查询

    db.places.find(
      {
         loc: {
           $geoWithin: { $polygon: [ [ 0 , 0 ], [ 3 , 6 ], [ 6 , 0 ] ] }
         }
      }
    )

    mongodb的空间位置查询

    我是勤劳的搬运工: -> https://docs.mongodb.com/manual/reference/operator/query-geospatial/

  • 相关阅读:
    SQL查询效率100w数据查询只要1秒
    超级实用且不花哨的js代码大全 (四) JavaScript[对象.属性]集锦
    Sql Server实用操作维护小技巧集合
    asp.net截取字符串方法
    自己整理的asp.net 缓存 相关资料
    【译】初识SSRS 通向报表服务的阶梯系列(一)
    【译】无处不在的数据 通向报表服务的阶梯系列(三)
    【译】SSRS基础 通向报表服务的阶梯系列(二)
    浅谈SQL Server中的事务日志(三)在简单恢复模式下日志的角色
    SQL Server中生成测试数据
  • 原文地址:https://www.cnblogs.com/wenbronk/p/7217148.html
Copyright © 2011-2022 走看看