Docs Menu
Docs Home
MongoDB Manual
/ /

Query a Haystack Index


Removed in MongoDB 5.0

MongoDB 5.0 removes the deprecated geoHaystack index and geoSearch command. Use a 2d index with $geoNear or one of the supported geospatial query operators instead.

Upgrading your MongoDB instance to 5.0 and setting featureCompatibilityVersion to 5.0 will delete any pre-existing geoHaystack indexes.

A haystack index is a special 2d geospatial index that is optimized to return results over small areas. To create a haystack index see Create a Haystack Index.

To query a haystack index, use the geoSearch command. You must specify both the coordinates and the additional field to geoSearch. For example, to return all documents with the value restaurant in the type field near the example point, the command would resemble:

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


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.


Spherical query operations are not currently supported by haystack indexes.

The find() method cannot access the haystack index.


Create a Haystack Index


Hashed Indexes