2d Index Internals索引内部构件

On this page本页内容

This document provides a more in-depth explanation of the internals of MongoDB's 2d geospatial indexes. 本文档更深入地解释了MongoDB的2d地理空间索引的内部结构。This material is not necessary for normal operations or application development but may be useful for troubleshooting and for further understanding.本材料对于正常操作或应用程序开发不是必需的,但对于故障排除和进一步理解可能有用。

Calculation of Geohash Values for 2d Indexes2d索引的Geohash值计算

When you create a geospatial index on legacy coordinate pairs, MongoDB computes geohash values for the coordinate pairs within the specified location range and then indexes the geohash values.传统坐标对上创建地理空间索引时,MongoDB会计算指定位置范围内坐标对的geohash值,然后对地理哈希数值进行索引。

To calculate a geohash value, recursively divide a two-dimensional map into quadrants. 要计算地理哈希值,请递归地将二维地图划分为象限。Then assign each quadrant a two-bit value. 然后为每个象限分配一个两位值。For example, a two-bit representation of four quadrants would be:例如,四个象限的两位表示为:

01  11
00  10

These two-bit values (00, 01, 10, and 11) represent each of the quadrants and all points within each quadrant. 这两个位值(00011011)表示每个象限和每个象限内的所有点。For a geohash with two bits of resolution, all points in the bottom left quadrant would have a geohash of 00. 对于具有两位分辨率的地理哈希,左下象限中的所有点的地理哈希都为00The top left quadrant would have the geohash of 01. 左上象限的地理哈希值为01The bottom right and top right would have a geohash of 10 and 11, respectively.右下角和右上角的地理哈希值分别为1011

To provide additional precision, continue dividing each quadrant into sub-quadrants. 要提供额外的精度,请继续将每个象限划分为子象限。Each sub-quadrant would have the geohash value of the containing quadrant concatenated with the value of the sub-quadrant. 每个子象限将包含象限的地理哈希值与子象限的值连接。The geohash for the upper-right quadrant is 11, and the geohash for the sub-quadrants would be (clockwise from the top left): 1101, 1111, 1110, and 1100, respectively.右上象限的地理哈希值为11,子象限的几何哈希值(从左上角顺时针)分别为:1101111111101100

Multi-location Documents for 2d Indexes2d索引的多位置文档

Note注意

While 2d geospatial indexes do not support more than one geospatial field in a document, you can use a multi-key index to index multiple coordinate pairs in a single document. 虽然2d地理空间索引不支持文档中的多个地理空间字段,但可以使用多键索引为单个文档中的坐标对编制索引。In the simplest example you may have a field (e.g. locs) that holds an array of coordinates, as in the following example:在最简单的示例中,您可能有一个保存坐标数组的字段(例如locs),如下例所示:

db.places.save( {
  locs : [ [ 55.5 , 42.3 ] ,
           [ -74 , 44.74 ] ,
           { lng : 55.5 , lat : 42.3 } ]
} )

The values of the array may be either arrays, as in [ 55.5, 42.3 ], or embedded documents, as in { lng : 55.5 , lat : 42.3 }.数组的值可以是数组,如[ 55.5, 42.3 ],也可以是嵌入文档,如{ lng : 55.5 , lat : 42.3 }

You could then create a geospatial index on the locs field, as in the following:然后,可以在locs字段上创建地理空间索引,如下所示:

db.places.createIndex( { "locs": "2d" } )

You may also model the location data as a field inside of an embedded document. 您还可以将位置数据建模为嵌入文档中的字段。In this case, the document would contain a field (e.g. addresses) that holds an array of documents where each document has a field (e.g. loc:) that holds location coordinates. 在这种情况下,文档将包含一个字段(例如addresses),该字段保存一个文档数组,其中每个文档都有一个保存位置坐标的字段(例如loc:)。For example:例如:

db.records.save( {
  name : "John Smith",
  addresses : [ {
                 context : "home" ,
                 loc : [ 55.5, 42.3 ]
                } ,
                {
                 context : "work",
                 loc : [ -74 , 44.74 ]
                }
              ]
} )

You could then create the geospatial index on the addresses.loc field as in the following example:然后,可以在addresses.loc字段上创建地理空间索引,如下例所示:

db.records.createIndex( { "addresses.loc": "2d" } )
←  Query a 2d IndexCalculate Distance Using Spherical Geometry →