MongoDB 5.0 removes the deprecated geoHaystack index and MongoDB 5.0删除了不推荐使用的geoSearch command. geoHaystack索引和geoSearch命令。Use a 2d index with 改为使用$geoNear or one of the supported geospatial query operators instead.2d索引和$geoNear或支持的地理空间查询运算符之一。
Upgrading your MongoDB instance to 5.0 and setting featureCompatibilityVersion to 将MongoDB实例升级到5.0并将5.0 will delete any pre-existing geoHaystack indexes.featureCompatibilityVersion设置为5.0将删除任何预先存在的geoHaystack索引。
A haystack index must reference two fields: the location field and a second field. 干草堆索引必须引用两个字段:位置字段和第二个字段。The second field is used for exact matches. Haystack indexes return documents based on location and an exact match on a single additional criterion. 第二个字段用于精确匹配。Haystack索引基于位置和单个附加标准的精确匹配返回文档。These indexes are not necessarily suited to returning the closest documents to a particular location.这些索引不一定适合将最近的文档返回到特定位置。
To build a haystack index, use the following syntax:要构建干草堆索引,请使用以下语法:
db.coll.createIndex( { <location field> : "geoHaystack" ,
<additional field> : 1 } ,
{ bucketSize : <bucket value> } )
To build a haystack index, you must specify the 要构建干草堆索引,必须在创建索引时指定bucketSize option when creating the index. bucketSize选项。A bucketSize of 5 creates an index that groups location values that are within 5 units of the specified longitude and latitude. bucketSize为5会创建一个索引,该索引对指定经度和纬度5个单位内的位置值进行分组。The bucketSize also determines the granularity of the index. bucketSize还决定了索引的粒度。You can tune the parameter to the distribution of your data so that in general you search only very small regions. 您可以根据数据的分布调整参数,以便通常只搜索非常小的区域。The areas defined by buckets can overlap. A document can exist in multiple buckets.桶定义的区域可以重叠。一个文档可以存在于多个存储桶中。
If you have a collection with documents that contain fields similar to the following:如果您有一个包含类似以下字段的文档集合:
{ _id : 100, pos: { lng : 126.9, lat : 35.2 } , type : "restaurant"}
{ _id : 200, pos: { lng : 127.5, lat : 36.1 } , type : "restaurant"}
{ _id : 300, pos: { lng : 128.0, lat : 36.7 } , type : "national park"}
The following operations create a haystack index with buckets that store keys within 1 unit of longitude or latitude.以下操作使用存储经度或纬度1个单位内的键的桶创建一个草堆索引。
db.places.createIndex( { pos : "geoHaystack", type : 1 } ,
{ bucketSize : 1 } )
This index stores the document with an 该索引将带有值为_id field that has the value 200 in two different buckets:200的_id字段的文档存储在两个不同的桶中:
_id field has a value of 100_id字段的值为100_id field has a value of 300_id字段的值为300To query using a haystack index you use the 要使用干草堆索引进行查询,请使用geoSearch command. geoSearch命令。See Query a Haystack Index.请参见查询干草堆索引。
By default, queries that use a haystack index return 50 documents.默认情况下,使用haystack索引的查询返回50个文档。