地理位置索引的概念
将一些点的位置存储在mongoDB中,创建索引后,可以依照位置来查找这些点.
地舆位置索引的分类2d索引,用于存储和查找平面上的点 平面地舆位置索引
2dsphere索引,用于存储和查找球面上的点. 球面地舆位置索引
地舆位置索引的查找方式:
地理位置索引-2d索引的创立方式
db.location.ensureIndex({w:"2d"})
创建了地理位置索引,mongoDB不允许查询跨越180的值
地舆位置索引-2d索引查询方式
地舆位置索引-2d索引-$neardb.location.find({w:{$near:[1,1]}})
$near会返回最近的100个记录.
地舆位置索引-2d索引-$near 限制返回的距离的远近$minDistance $maxDistancedb.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,[2,3]]}}})
地舆位置索引-2d索引 $geoWithin 查询圆形中的点db.location.find({w:{$geoWithin:{$center:[[0,5]}}})
地舆位置索引-2d索引 $geoWithin 查询多边形中的点db.location.find({w:{$geoWithin:{$polygon:[[0,[0,5],[6,1]]}}})
地舆位置索引-2d索引 geoNear
db.runCommand({geoNear:"location",near:[1,2],maxDistance:10,num:1})
地舆位置索引-2dsphere索引