MongoDB Geo-Spatial index

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.