mongodb地理位置查询(mongoDB地理位置索引)

地理位置索引的概念

将一些点的位置存储在mongoDB中,创建索引后,可以按照位置来查找这些点.

地理位置索引的分类
  1. 2d索引,用于存储和查找平面上的点 平面地理位置索引

  2. 2dsphere索引,用于存储和查找球面上的点. 球面地理位置索引

地理位置索引的查找方式:

地理位置索引-2d索引的创建方式

db.location.ensureIndex({w:"2d"})

创建了地理位置索引,mongoDB不允许查询超过180的值

地理位置索引-2d索引查询方式

地理位置索引-2d索引-$near

db.location.find({w:{$near:[1,1]}})

$near会返回最近的100个记录.

地理位置索引-2d索引-$near 限制返回的距离的远近$minDistance $maxDistance

db.location.find({w:{$near:[1,1],$minDistance:2,$maxDistance:10}})

限制最远距离:

限制最近距离:

最远和最近距离都限制:

地理位置索引-2d索引 $geoWithin 形状的表示

由于$geoWithin是查询某个形状内的点,所以先要学会如何表示形状.

地理位置查询

地理位置索引-2d索引 $geoWithin 查询矩形中的点

db.location.find({w:{$geoWithin:{$box:[[0,0],[3,3]]}}})

db.location.find({w:{$geoWithin:{$box:[[1,1],[2,3]]}}})

地理位置索引-2d索引 $geoWithin 查询圆形中的点

db.location.find({w:{$geoWithin:{$center:[[0,0],5]}}})

地理位置索引-2d索引 $geoWithin 查询多边形中的点

db.location.find({w:{$geoWithin:{$polygon:[[0,0],[0,1],[2,5],[6,1]]}}})

地理位置索引-2d索引 geoNear

db.runCommand({geoNear:"location",near:[1,2],maxDistance:10,num:1})

地理位置索引-2dsphere索引

您可以还会对下面的文章感兴趣

最新评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

使用微信扫描二维码后

点击右上角发送给好友