Query a Haystack Index
A haystack index is a special 2d
geospatial index that is optimizedto return results over small areas. To create a haystack index seeCreate a Haystack Index.
To query a haystack index, use the geoSearch
command. Youmust specify both the coordinates and the additionalfield to geoSearch
. For example, to return all documentswith the value restaurant
in the type
field near the examplepoint, the command would resemble:
- 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 ofdocuments closest to a particular location. The closest documentscould be more distant compared to the bucket size.
Note
Spherical query operationsare not currently supported by haystack indexes.
The find()
method cannot access thehaystack index.