Mongodb地理堆栈与标准的Mongodb空间索引相同吗



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

http://www.mongodb.org/display/DOCS/Geospatial+索引

标准的。附带说明:

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

然后就是所谓的"地球干草堆"。

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

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

那有什么不同呢?

Mongodb似乎不使用Rtree之类的东西,对吧?

NB:关于MongoDB是如何实现的问题的回答';s的空间索引?说2d索引也使用geohash。

实现类似,但用例差异在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参数是必需的,用于确定干草堆索引的粒度。

例如:

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

这个示例bucketSize为1创建了一个索引,其中经度或纬度1个单位内的键存储在同一个bucket中。索引中还可以包括一个额外的类别,这意味着在查找位置详细信息的同时将查找信息。

B树表示类似于:

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

如果您的用例通常搜索"附近"的位置(即"25英里内的餐馆"),则草堆索引可能更有效。附加索引字段(如类别)的匹配项可以在每个bucket中找到并计数。

相反,如果您正在搜索"最近的餐厅",并且希望无论距离远近都返回结果,则正常的2d索引将更有效。

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

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

注意:纬度之间的距离变化很大(经度较小)。请看:一个纬度和经度之间的距离是多少?。

最新更新