geoHaystack Indexes

On this page本页内容

Important重要
Removed in MongoDB 5.0已在MongoDB 5.0中删除

MongoDB 5.0 removes the deprecated geoHaystack index and geoSearch command. MongoDB 5.0删除了不推荐使用的geoHaystack索引和geoSearch命令。Use a 2d index with $geoNear or one of the supported geospatial query operators instead.改为使用带有$geoNear2d索引或支持的地理空间查询运算符之一。

Upgrading your MongoDB instance to 5.0 and setting featureCompatibilityVersion to 5.0 will delete any pre-existing geoHaystack indexes.将MongoDB实例升级到5.0并将featureCompatibilityVersion设置为5.0将删除任何预先存在的geoHaystack索引。

A geoHaystack index is a special index that is optimized to return results over small areas. geoHaystack索引是一种特殊的索引,可以优化以返回小区域的结果。geoHaystack indexes improve performance on queries that use flat geometry.geoHaystack索引提高了使用平面几何体的查询的性能。

For queries that use spherical geometry, a 2dsphere index is a better option than a haystack index. 对于使用球面几何体的查询,2dsphere索引比haystack索引更好2dsphere indexes allow field reordering; geoHaystack indexes require the first field to be the location field. 2dsphere索引允许字段重新排序;geoHaystack索引要求第一个字段是位置字段。Also, geoHaystack indexes are only usable via commands and so always return all results at once.此外,geoHaystack索引只能通过命令使用,因此总是一次返回所有结果。

Behavior行为

geoHaystack indexes create "buckets" of documents from the same geographic area in order to improve performance for queries limited to that area. 索引创建来自同一地理区域的文档“bucket”,以提高仅限于该区域的查询的性能。Each bucket in a geoHaystack index contains all the documents within a specified proximity to a given longitude and latitude.geoHaystack索引中的每个bucket都包含给定经度和纬度指定范围内的所有文档。

sparse Property属性

geoHaystack indexes are sparse by default and ignore the sparse: true option. 默认情况下,geoHaystack索引是稀疏的,并忽略sparse:true选项。If a document lacks a geoHaystack index field (or the field is null or an empty array), MongoDB does not add an entry for the document to the geoHaystack index. 如果文档缺少geoHaystack索引字段(或该字段为null或空数组),MongoDB不会将该文档的条目添加到geoHaysack索引中。For inserts, MongoDB inserts the document but does not add to the geoHaystack index.对于插入,MongoDB会插入文档,但不会添加到geoHaystack索引中。

geoHaystack indexes include one geoHaystack index key and one non-geospatial index key; however, only the geoHaystack index field determines whether the index references a document.索引包括一个geoHaystack索引键和一个非地理空间索引键;但是,只有geoHaystack索引字段确定索引是否引用文档。

Collation Option排序规则选项

geoHaystack indexes only support simple binary comparison and do not support the collation.索引只支持简单的二进制比较,不支持排序规则

To create a geoHaystack index on a collection that has a non-simple collation, you must explicitly specify {collation: {locale: "simple"} } when creating the index.要在具有非简单排序规则的集合上创建geoHaystack索引,必须在创建索引时显式指定{collation: {locale: "simple"} }

Create geoHaystack Index创建geoHaystack索引

To create a geoHaystack index, see Create a Haystack Index. 要创建geoHaystack索引,请参阅创建干草堆索引For information and example on querying a haystack index, see Query a Haystack Index.有关查询干草堆索引的信息和示例,请参阅查询干草垛索引

←  Calculate Distance Using Spherical GeometryCreate a Haystack Index →