定義
$geoWithin指定した形状内に完全に存在する地理空間データを含むドキュメントを選択します。
The specified shape can be either a GeoJSON
Polygon(either single-ringed or multi-ringed), a GeoJSONMultiPolygon, or a shape defined by legacy coordinate pairs. The$geoWithinoperator uses the$geometryoperator to specify the GeoJSON object.デフォルトの座標参照システム(CRS)を使用して GeoJSON 多角形またはマルチポリゴンを指定するには、次の構文を使用します。
{ <location field>: { $geoWithin: { $geometry: { type: <"Polygon" or "MultiPolygon"> , coordinates: [ <coordinates> ] } } } } For
$geoWithinqueries that specify GeoJSON geometries with areas greater than a single hemisphere, the use of the default CRS results in queries for the complementary geometries.カスタム MongoDB CRS を使用して単一リングの GeoJSON多角形を指定するには、
$geometry式でカスタム MongoDB CRS を指定する次のプロトタイプを使用します。{ <location field>: { $geoWithin: { $geometry: { type: "Polygon" , coordinates: [ <coordinates> ], crs: { type: "name", properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" } } } } } } The custom MongoDB CRS uses a counter-clockwise winding order and allows
$geoWithinto support queries with a single-ringed GeoJSON polygon whose area is greater than or equal to a single hemisphere. If the specified polygon is smaller than a single hemisphere, the behavior of$geoWithinwith the MongoDB CRS is the same as with the default CRS. See also "Big" Polygons.平面上の legacy coordinate pairs で定義された形状に含めるかどうかをクエリする場合は、次の構文を使用します。
{ <location field>: { $geoWithin: { <shape operator>: <coordinates> } } } 利用可能な形状演算子は次のとおりです。
$center(円を定義します)、そして$centerSphere(球面上の円を定義します)。
重要
経度と緯度を使用する場合は、
longitude, latitudeの順に座標を指定します。
動作
地理空間インデックス
$geoWithin does not require a geospatial index. However, a geospatial index will improve query performance. Both 2dsphere and 2d geospatial indexes support $geoWithin.
ソートされていない結果
The $geoWithin operator does not return sorted results. As such, MongoDB can return $geoWithin queries more quickly than geospatial $near or $nearSphere queries, which sort results.
退化ジオメトリ
$geoWithin ジオメトリの一部がそのコンポーネント ジオメトリを含むと見なされたり、コンポーネント ジオメトリを共有する別の多角形と見なされたりすることを保証するものではありません。
"大きな" 多角形
For $geoWithin, if you specify a single-ringed polygon that has an area greater than a single hemisphere, include the custom MongoDB coordinate reference system in the $geometry expression. Otherwise, $geoWithin queries for the complementary geometry. For all other GeoJSON polygons with areas greater than a hemisphere, $geoWithin queries for the complementary geometry.
例
多角形内
次の例では、GeoJSON Polygon 内に完全に存在するすべての loc データを選択します。多角形の面積は 1 つの半球の面積よりも小さくなります。
db.places.find( { loc: { $geoWithin: { $geometry: { type : "Polygon" , coordinates: [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ] } } } } )
単一の半球よりも大きい面積を持つ単一リング多角形については、 「大きな」多角形内を参照してください。
「大きな」多角形内
単一の半球よりも大きい面積を持つ単一リングの GeoJSON 多角形でクエリを実行するには、 $geometry式でカスタム MongoDB 座標参照システムを指定する必要があります。 例:
db.places.find( { loc: { $geoWithin: { $geometry: { type : "Polygon" , coordinates: [ [ [ -100, 60 ], [ -100, 0 ], [ -100, -60 ], [ 100, -60 ], [ 100, 60 ], [ -100, 60 ] ] ], crs: { type: "name", properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" } } } } } } )