The principle illustrated MongoDB geographical index

Location Index MongoDB support is a major attraction, which is the world's most popular LBS services foursquare select one of the reasons for MongoDB. We know, the usual database index structure is B + Tree, how can build location into the form + B Tree, you will describe below.

We will first assume that the entire map to index into 16 × 16 squares, as shown below (the lower left corner to the upper right corner coordinates as coordinates 0,0 16, 16):

 

 

 

Simple [x, y] data is not indexed, so MongoDB in the indexing time, will be calculated based on the coordinates of the corresponding field can be used to make a hash index value, which is called geohash , in order to let the map point (the position in FIG red cross) coordinates [4,6] for example.

Our first step will be the size of the entire map is divided into four and so on, as shown below:

 

 

 

After divided into four we can define these four values, as follows (lower left is 00, 01 for the upper left, lower right 10, upper right 11):

01 11
00 10


Then cut four pieces each block, as follows: so that [4,6] geohash point value is currently 00

At this time [4,6] point in the upper right region, the upper right is 11, geohash value thus [4,6] point becomes: 0011

Continue down twice segmentation:

 

 

 

 

Finally obtained [4,6] geohash point is: 00110100

We do so with the index value, the point on the map similar point can be converted into geohash values ​​are the same prefix.

We can see that the accuracy of this geohash value is proportional to the number of divided map, the map is divided on the cases of four. The default is 26 MongoDB division, the value when indexing is controllable. Command to establish specific two-dimensional geographic index is as follows:

db.map.ensureIndex({point : "2d"}, {min : 0, max : 16, bits : 4})

Which is divided into several bits parameter, the default is 26.

Guess you like

Origin www.cnblogs.com/xubao/p/12275656.html