Principle of MongoDB geographic location index implementation

Source: Internet
Author: User

Geographic location index support is one of the highlights of MongoDB, which is also one of the reasons why Foursquare, the world's most popular lbs service, chooses MongoDB. We know that the general database index structure is B + tree, and how to convert the geographical location into a form of B + tree, which will be described below.

First, let's divide the entire map to be indexed into 16 × 16 squares, for example, (the lower left corner is coordinate 0, the upper right corner is coordinate 16, 16 ):

 

Data of [x, y] alone cannot be indexed. Therefore, when MongoDB creates an index, it will calculate a hash value that can be used for Indexing Based on the coordinates of the corresponding field, the value is geohash. The following uses the coordinates of [4, 6] on the map (the Red Cross position in the figure) as an example.

The first step is to divide the entire map into four equal-Size Blocks, for example:

After dividing the data into four blocks, we can define the values of these four blocks, as shown below (00 in the lower left, 01 in the upper left, 10 in the lower right, and 11 in the upper right ):

 

01 11
00 10

 

In this way, the geohash value of [4, 6] is currently 00

Then we cut each of the four small blocks as follows:

In this case, [4, 6] points are located in the upper right area and the value on the upper right is 11. In this way, the geohash value of [4, 6] points is changed to: 0011.

Continue to perform two splitting operations:

Finally, the geohash value of [4, 6] Is 00110100.

In this way, the points with similar points on the map can be converted into geohash values with the same prefix.

We can see that the accuracy of this geohash value is proportional to the number of Map Division times. The previous example divided the map four times. MongoDB is divided into 26 times by default. This value is controllable during index creation. The command to create a two-dimensional geographic location index is as follows:

DB. Map. ensureindex ({point: "2D"}, {min: 0, Max: 16, Bits: 4 })

The bits parameter is divided several times. The default value is 26.

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.