MongoDB 地理索引

查詢區域內的點

db.<collection>.find( { <location field> :
                         { $geoWithin :
                           { $geometry :
                             { type : "Polygon" ,
                               coordinates : [ <coordinates> ]
                      } } } } )
 
 
db.realTimeGpsLocation.find({ 
  				point :
                  { $geoWithin :
                    { $geometry :
                      { type : "Polygon" ,
                        coordinates : [ 
                                        [
                                          [120.1234,30.257822 ],
                                          [120.1568,30.275555 ],
                                          [120.910717,29.987169 ],
                                          [120.860437,29.947169 ],
                                          [120.870437,30.252122 ]
                                        ] 
                                      ]
                } } } })

查詢區域相交的點

db.<collection>.find( { <location field> :
                         { $geoIntersects :
                           { $geometry :
                             { type : "<GeoJSON object type>" ,
                               coordinates : [ <coordinates> ]
                      } } } } )


db.realTimeGpsLocation.find( { point :
                  { $geoIntersects :
                    { $geometry :
                      { type : "Polygon" ,
                        coordinates: [ 
                                        [
                                         [120.1234,30.257822 ],
                                          [120.1568,30.275555 ],
                                          [120.910717,29.987169 ],
                                          [120.860437,29.947169 ],
                                          [120.870437,30.252122 ]
                                        ] 
                                      ]
                } } } } )

查詢周邊幾公里內的點

db.<collection>.find( { <location field> :
                         { $near :
                           { $geometry :
                              { type : "Point" ,
                                coordinates : [ <longitude> , <latitude> ] } ,
                             $maxDistance : <distance in meters>
                      } } } )

db.realTimeGpsLocation.find( { point :
                         { $near :
                           { $geometry :
                              { type : "Point" ,
                                coordinates : [ 120.1234,30.257822 ] } ,
                                $maxDistance : 500
                            } } } )

 

參考

https://docs.mongodb.com/manual/tutorial/query-a-2dsphere-index/

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章