Create a Haystack Index

A haystack index must reference two fields: the location field and asecond field. The second field is used for exact matches. Haystackindexes return documents based on location and an exact match on asingle additional criterion. These indexes are not necessarily suitedto returning the closest documents to a particular location.

To build a haystack index, use the following syntax:

  1. db.coll.createIndex( { <location field> : "geoHaystack" ,
  2. <additional field> : 1 } ,
  3. { bucketSize : <bucket value> } )

To build a haystack index, you must specify the bucketSize optionwhen creating the index. A bucketSize of 5 creates an indexthat groups location values that are within 5 units of the specifiedlongitude and latitude. The bucketSize also determines thegranularity of the index. You can tune the parameter to thedistribution of your data so that in general you search only very smallregions. The areas defined by buckets can overlap. A document can existin multiple buckets.

Example

If you have a collection with documents that contain fields similar tothe following:

  1. { _id : 100, pos: { lng : 126.9, lat : 35.2 } , type : "restaurant"}
  2. { _id : 200, pos: { lng : 127.5, lat : 36.1 } , type : "restaurant"}
  3. { _id : 300, pos: { lng : 128.0, lat : 36.7 } , type : "national park"}

The following operations create a haystack index with buckets thatstore keys within 1 unit of longitude or latitude.

  1. db.places.createIndex( { pos : "geoHaystack", type : 1 } ,
  2. { bucketSize : 1 } )

This index stores the document with an _id field that has thevalue 200 in two different buckets:

  • In a bucket that includes the document where the _id field hasa value of 100
  • In a bucket that includes the document where the _id field hasa value of 300

To query using a haystack index you use the geoSearchcommand. See Query a Haystack Index.

By default, queries that use a haystack index return 50 documents.