2d
On this page本页内容
This document provides a more in-depth explanation of the internals of MongoDB's 本文档更深入地解释了MongoDB的2d
geospatial indexes. 2d
地理空间索引的内部结构。This material is not necessary for normal operations or application development but may be useful for troubleshooting and for further understanding.本材料对于正常操作或应用程序开发不是必需的,但对于故障排除和进一步理解可能有用。
2d
IndexesWhen 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. 00
、01
、10
和11
)表示每个象限和每个象限内的所有点。For a geohash with two bits of resolution, all points in the bottom left quadrant would have a geohash of 对于具有两位分辨率的地理哈希,左下象限中的所有点的地理哈希都为00
. 00
。The top left quadrant would have the geohash of 左上象限的地理哈希值为01
. 01
。The bottom right and top right would have a geohash of 右下角和右上角的地理哈希值分别为10
and 11
, respectively.10
和11
。
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
,子象限的几何哈希值(从左上角顺时针)分别为:1101
、1111
、1110
和1100
。
2d
Indexes2d
索引的多位置文档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" } )