1. Lbs geo-Spatial index
About lbs related items, generally store the latitude and longitude coordinates of each location, if you want to query nearby sites, you need to set up an index to improve query efficiency. MongoDB establishes a geospatial index specifically for such queries. 2d and 2dsphere indexes.
2. Create an index
Set up a places collection to store the location where the LOC field is used to store the regional data Geojson point.
Db.places.insert ( { loc: {type: ' point ', coordinates: [ -73.97, 40.77]}, name: "Central Park", Categor Y: "Parks" }) Db.places.insert ( { loc: {type: "point", Coordinates: [ -73.88, 40.78]}, name: "La Guard IA Airport ", Category:" Airport " })
Build an index
Db.places.ensureIndex ({loc: "2dsphere"})
The number of references is not 1 or-1, which is 2dsphere. You can also create composite indexes.
Db.places.ensureIndex ({loc: "2dsphere", Category:-1, name:1})
3. Enquiry
$geometry represents a geometric picture of the query.
3.1 Querying the value of a polygon range
Type representation: Polygon Polygon
Db.places.find ({loc: {$geoWithin: {$geometry: { type: "Polygon", coordinates: [[[ 0, 0] , [3, 6], [6, 1], [0, 0] ]}}} )
3.2 Values near the query
Use $near to find nearby locations.
Db.places.find ({loc: {$near: {$geometry: { type: ' point ', coordinates: [<longitude>, < ;latitude>]}, $maxDistance: <distance in Meters> }})
3.3 Querying values within a circle
When querying a circle, you need to specify the center, radius.
Db.places.find ({loc: {$geoWithin: {$centerSphere: [[ -88, +], +}}} )
[-88, 30] for latitude and longitude, 10 for radius.
Address: http://blog.csdn.net/yonggang7/article/details/28109463
MongoDB geo-spatial index