• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Mongodb地理空间索引

mysql 搞代码 4年前 (2022-01-09) 38次浏览 已收录 0个评论
文章目录[隐藏]

1. LBS地理空间索引 关于LBS相关项目,一般存储每个地点的经纬度的坐标, 如果要查询附近的场所,则需要建立索引来提升查询效率。 Mongodb专门针对这种查询建立了地理空间索引。 2d和2dsphere索引。 2. 创建索引 建立places集合,来存放地点, loc字段用来存

1. LBS地理空间索引

关于LBS相关项目,一般存储每个地点的经纬度的坐标, 如果要查询附近的场所,则需要建立索引来提升查询效率。 Mongodb专门针对这种查询建立了地理空间索引。 2d和2dsphere索引。

2. 创建索引

建立places集合,来存放地点, loc字段用来存放地区数据GeoJSON Point。

db.places.insert(   {      loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },      name: "Central Park",      category : "Parks"   })db.places.insert(   {      loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },      name: "La Guardia Airport",      category : "Airport"   })

建立索引

db.places.ensureIndex( { loc : "2dsphere" } )

参数不是1或-1,为2dsphere。还可以建立组合索引。

db.places.ensureIndex( { loc : "2dsphere" , category : -1, name: 1 } )

3. 查询

$geometry表示查询的几何图片.

3.1 查询多边形范围的值

type表示类型:polygon 多边形

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

3.2 查询附近的值

使用$near来查询附近的地点。

 db.places.find( { loc :                    <strong style="color:transparent">本文来源gaodai#ma#com搞@@代~&码网^</strong>     { $near :                           { $geometry :                              { type : "Point" ,                                coordinates : [  ,  ] } ,                             $maxDistance :                       } } } )

3.3 查询圆形内的值

查询圆时,需要指定圆心, 半径。

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

[-88, 30] 为经纬度, 10为半径。
地址:http://blog.gaodaima.com/yonggang7/article/details/28109463


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Mongodb地理空间索引

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址