mongoDB地理位置索引

NoSQL MongoDB 地理 技術 從頭開始自學java 2017-06-05

地理位置索引的概念

將一些點的位置存儲在mongoDB中,創建索引後,可以按照位置來查找這些點.

地理位置索引的分類

  1. 2d索引,用於存儲和查找平面上的點 平面地理位置索引

  2. 2dsphere索引,用於存儲和查找球面上的點. 球面地理位置索引

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

mongoDB地理位置索引

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

mongoDB地理位置索引

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

創建了地理位置索引,mongoDB不允許查詢超過180的值

mongoDB地理位置索引

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

mongoDB地理位置索引

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

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

mongoDB地理位置索引

$near會返回最近的100個記錄.

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

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

限制最遠距離:

mongoDB地理位置索引

限制最近距離:

mongoDB地理位置索引

最遠和最近距離都限制:

mongoDB地理位置索引

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

由於$geoWithin是查詢某個形狀內的點,所以先要學會如何表示形狀.

mongoDB地理位置索引

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

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

mongoDB地理位置索引

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

mongoDB地理位置索引

地理位置索引-2d索引 $geoWithin 查詢圓形中的點

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

mongoDB地理位置索引

地理位置索引-2d索引 $geoWithin 查詢多邊形中的點

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

mongoDB地理位置索引

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

mongoDB地理位置索引

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

mongoDB地理位置索引

地理位置索引-2dsphere索引

mongoDB地理位置索引

相關推薦

推薦中...