geoHaystack Indexes

A geoHaystack index is a special index that is optimized to returnresults over small areas. geoHaystack indexes improve performanceon queries that use flat geometry.

For queries that use spherical geometry, a 2dsphere index is a betteroption than a haystack index. 2dsphere indexes allow field reordering; geoHaystack indexesrequire the first field to be the location field. Also, geoHaystackindexes are only usable via commands and so always return all resultsat once.

Behavior

geoHaystack indexes create “buckets” of documents from the samegeographic area in order to improve performance for queries limited tothat area. Each bucket in a geoHaystack index contains all thedocuments within a specified proximity to a given longitude andlatitude.

sparse Property

geoHaystack indexes are sparse bydefault and ignore the sparse: trueoption. If a document lacks a geoHaystack index field (or the fieldis null or an empty array), MongoDB does not add an entry for thedocument to the geoHaystack index. For inserts, MongoDB inserts thedocument but does not add to the geoHaystack index.

geoHaystack indexes include one geoHaystack index key and onenon-geospatial index key; however, only the geoHaystack index fielddetermines whether the index references a document.

Collation Option

geoHaystack indexes only support simple binary comparison and do not supportthe collation.

To create a geoHaystack index on a a collection that has a non-simplecollation, you must explicitly specify {collation: {locale: "simple"}} when creating the index.

Create geoHaystack Index

To create a geoHaystack index, seeCreate a Haystack Index. For information and exampleon querying a haystack index, seeQuery a Haystack Index.