查詢區域內的點
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/