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:

  1. db.runCommand( { geoSearch : "places" ,
  2. search : { type: "restaurant" } ,
  3. near : [-74, 40.74] ,
  4. 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.