Mongodb geohaystack与标准mongodb空间索引相同吗?

时间:2012-09-14 05:38:22

标签: mongodb indexing

似乎mongodb有两种地理空间索引。

http://www.mongodb.org/display/DOCS/Geospatial+Indexing

标准的。附注:

  

目前,每个集合可能只有1个地理空间索引。而   MongoDB可能允许创建多个索引,这种行为是   不支持的。因为MongoDB只能使用一个索引来支持   在大多数情况下,单个查询将产生多个地理索引   不良行为。

然后就是这个所谓的geohaystack thingy。

http://www.mongodb.org/display/DOCS/Geospatial+Haystack+Indexing

他们都声称使用相同的算法。他们都把地球变成了几个网格。然后根据它进行搜索。

那有什么不同?

Mongodb似乎没有使用Rtree和东西吗?

注意:回答这个问题How does MongoDB implement it's spatial indexes?说2d索引也使用geohash。

1 个答案:

答案 0 :(得分:3)

实现类似,但用例差异在Geospatial Haystack Indexing页面上描述。

干草堆指数是针对小区域经度/纬度搜索而调整的“基于桶”(又名“象限”)搜索:

    In addition to ordinary 2d geospatial indices, mongodb supports the use
    of bucket-based geospatial indexes. Called "Haystack indexing", these
    indices can accelerate small-region type longitude / latitude queries
    when additional criteria is also required.

    For example, "find all restaurants within 25 miles with name 'foo'".

    Haystack indices allow you to tune your bucket size to the distribution
    of your data, so that in general you search only very small regions of
    2d space for a particular kind of document.  They are not suited for
    finding the closest documents to a particular location, when the
    closest documents are far away compared to bucket size.

bucketSize参数是必需的,它决定了haystack索引的粒度。

所以,例如:

 db.places.ensureIndex({ pos : "geoHaystack", type : 1 }, { bucketSize : 1 })

此示例bucketSize为1创建一个索引,其中1个经度或纬度单位的密钥存储在同一个存储桶中。索引中还可以包含其他类别,这意味着将在查找位置详细信息的同时查找信息。

B树表示类似于:

 { loc: "x,y", category: z }

如果您的用例通常搜索“附近”位置(即“25英里范围内的餐馆”),则干草堆索引可能更有效。可以在每个存储桶中找到并计算其他索引字段(例如类别)的匹配。

相反,如果您正在搜索“最近的餐厅”并且想要返回结果,无论距离如何,正常的2d索引将更有效。

目前(截至MongoDB 2.2.0)对干草堆索引有一些限制:

  • 干草堆索引中只能包含一个附加字段
  • 附加索引字段必须是单个值,而不是数组
  • 不支持null long / lat值

注意:纬度之间的距离会有很大差异(经度,不那么)。请参阅:What is the distance between a degree of latitude and longitude?