実装は似ていますが、ユースケースの違いは地理空間ヘイスタックインデックスページで説明されています。
干し草の山のインデックスは、小地域の経度/緯度の検索用に調整された「バケットベース」(別名「象限」)の検索です。
In addition to ordinary 2d geospatial indices, mongodb supports the use
of bucket-based geospatial indexes. Called "Haystack indexing", these
indices can accelerate small-region type longitude / latitude queries
when additional criteria is also required.
For example, "find all restaurants within 25 miles with name 'foo'".
Haystack indices allow you to tune your bucket size to the distribution
of your data, so that in general you search only very small regions of
2d space for a particular kind of document. They are not suited for
finding the closest documents to a particular location, when the
closest documents are far away compared to bucket size.
このbucketSize
パラメーターは必須であり、干し草の山のインデックスの粒度を決定します。
したがって、たとえば:
db.places.ensureIndex({ pos : "geoHaystack", type : 1 }, { bucketSize : 1 })
この例のbucketSizeが1の場合、経度または緯度の1単位以内のキーが同じバケットに格納されるインデックスが作成されます。追加のカテゴリをインデックスに含めることもできます。これは、場所の詳細を見つけると同時に情報が検索されることを意味します。
Bツリー表現は次のようになります。
{ loc: "x,y", category: z }
ユースケースで通常「近くの」場所(つまり、「25マイル以内のレストラン」)を検索する場合は、干し草の山のインデックスの方が効率的です。追加のインデックス付きフィールド(カテゴリなど)の一致は、各バケット内で検索およびカウントできます。
代わりに、「最寄りのレストラン」を検索していて、距離に関係なく結果を返したい場合は、通常の2Dインデックスの方が効率的です。
現在(MongoDB 2.2.0の時点で)、haystackインデックスにはいくつかの制限があります。
- 干し草の山のインデックスに含めることができる追加のフィールドは1つだけです
- 追加のインデックスフィールドは、配列ではなく単一の値である必要があります
- nullのlong/lat値はサポートされていません
注:緯度間の距離は大きく異なります(経度、それほどではありません)。参照:緯度と経度の間の距離はどれくらいですか?。