2dsphere インデックスの作成
2dsphere インデックスは、地球のような球体上の地理空間クエリをサポートします。たとえば、2dsphere インデックスでは次のことが可能です。
指定したエリア内のポイントを決定します。
指定した点への近接度を計算します。
座標クエリで完全一致を返します。
2dsphere インデックスを作成するには、db.collection.createIndex()
{4 メソッドを使用し、インデックスstring"2dsphere"
タイプとして を指定します。
db.<collection>.createIndex( { <location field> : "2dsphere" } )
<location field>
の値は次のいずれかである必要があります。
始める前に
これらのドキュメントを含むplaces
コレクションを作成します。
db.places.insertMany( [ { loc: { type: "Point", coordinates: [ -73.97, 40.77 ] }, name: "Central Park", category : "Park" }, { loc: { type: "Point", coordinates: [ -73.88, 40.78 ] }, name: "La Guardia Airport", category: "Airport" }, { loc: { type: "Point", coordinates: [ -1.83, 51.18 ] }, name: "Stonehenge", category : "Monument" } ] )
loc
フィールドの値はGeoJSON ポイントです。
手順
次の操作は、ロケーション フィールドloc
に2 dsphere インデックスを作成します。
db.places.createIndex( { loc : "2dsphere" } )
次のステップ
2 dsphere インデックスを作成したら、そのインデックスを地理空間クエリに使用できます。 詳しくは、「 2 dsphere インデックスのクエリ 」を参照してください。