Query a Haystack Index

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.改为使用带有$geoNear支持的2d索引或受支持的地理空间查询运算符之一。

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 haystack index is a special 2d geospatial index that is optimized to return results over small areas. 干草堆索引是一种特殊的2d地理空间索引,经过优化后可返回小区域的结果。To create a haystack index see Create a Haystack Index.要创建干草堆索引,请参阅创建干草垛索引

To query a haystack index, use the geoSearch command. 要查询干草堆索引,请使用geoSearch命令。You must specify both the coordinates and the additional field to geoSearch. 您必须为geoSearch指定坐标和附加字段。For example, to return all documents with the value restaurant in the type field near the example point, the command would resemble:例如,要在示例点附近的type字段中返回值为restaurant的所有文档,命令如下:

db.runCommand( { geoSearch : "places" ,
                 search : { type: "restaurant" } ,
                 near : [-74, 40.74] ,
                 maxDistance : 10 } )
Note注意

Haystack indexes are not suited to queries for the complete list of documents closest to a particular location. 干草堆索引不适合查询离特定位置最近的完整文档列表。The closest documents could be more distant compared to the bucket size.与桶大小相比,最近的文档可能更远。

Note注意

Spherical query operations球形查询操作 are not currently supported by haystack indexes.草堆索引当前不支持。

The find() method cannot access the haystack index.find()方法无法访问干草堆索引。

←  Create a Haystack IndexHashed Indexes →