AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

geoWithin 演算子

geoWithin

geoWithin演算子は、指定された形状内の地理的ポイントのクエリをサポートしています。 インデックス定義でindexShapesの値がtrueであっても、ポイントのみが返されます。

以下内のポイントをクエリできます。

  • 円

  • 境界ボックス

  • 多角形

検索する座標を指定する場合は、最初に 経度 、次に 緯度 を指定する必要があります。 経度の値は、両方を含む-180と180の間で指定できます。 緯度の値は-90と90の間で指定できます。 座標値は整数または double にすることができます。

注意

MongoDB Search は以下の機能をサポートしていません。

  • 非デフォルトの座標参照システム(CRS)

  • 平面 XY 座標系 (2 次元)

  • 座標ペアのポイント表記(つまりpointFieldName: [12, 34] )

geoWithin の構文は次のとおりです。

{
"$search": {
"index": <index name>, // optional, defaults to "default"
"geoWithin": {
"path": "<field-to-search>",
"box | circle | geometry": <object>,
"score": <score-options>
}
}
}

geoWithin では、次の用語を使用してクエリを作成します。

フィールド
タイプ
説明
必要性

box

オブジェクト

Object that specifies the bottom left and top right GeoJSON points of a box to search within. The object takes the following fields:

  • bottomLeft - Bottom left GeoJSON point.

  • topRight - Top right GeoJSON point.

GeoJSONオブジェクト内で GeoJSON データを指定する方法については、GeoJSON オブジェクトを参照してください。

box 、 circle 、またはgeometryのいずれかが必要です。

条件付き

circle

オブジェクト

検索する中心点と半径をメートル単位で指定するオブジェクト。 オブジェクトには、次のGeoJSONフィールドが含まれています。

  • center - Center of the circle specified as a GeoJSON point.

  • radius - 半径(メートル単位で指定される数値)。 値は0以上である必要があります。

GeoJSONオブジェクト内で GeoJSON データを指定する方法については、GeoJSON オブジェクトを参照してください。

circle 、 box 、またはgeometryのいずれかが必要です。

条件付き

geometry

GeoJSON オブジェクト

検索対象の Multi ポリゴンまたは 多角形 を指定する GeoJSON オブジェクト。多角形は最後の位置が最初の位置と同じである閉じたループとして指定する必要があります。

When calculating geospatial results, MongoDB Search geoShape and geoWithin operators and MongoDB $geoIntersects operator use different geometries. This difference can be seen in how MongoDB Search and MongoDB draw polygonal edges.

MongoDB Search は、座標参照システム内の 2 点間の最短線である直列距離に基づいて多角形を描画します。

MongoDB draws polygons using the geodesic mode based on 2dsphere indexes that is built on top of a third-party library for geodesic types, or the flat mode, from 2d indexes. To learn more, see GeoJSON Objects.

MongoDB Search とMongoDBでは、多角形に関係する地理空間クエリで異なる結果が返される可能性がありました。

GeoJSONオブジェクト内で GeoJSON データを指定する方法については、GeoJSON オブジェクトを参照してください。

geometry 、 box 、またはcircleのいずれかが必要です。

条件付き

path

文字列または複数の文字列の配列

検索するインデックス付き geo 型フィールド。

はい

score

オブジェクト

一致する検索結果に割り当てる スコア 。 デフォルトでは、結果のスコアは1です。 次のオプションを使用してスコアを変更できます。

  • boost: 結果のスコアに指定された数値を掛けます。

  • constant: 結果のスコアを指定された数値に置き換えます。

  • function: 結果のスコアを指定された式で置き換えます。

クエリで score を使用する方法については、「結果内のドキュメントのスコアリング」を参照してください。

no

The following examples use the listingsAndReviews collection in the sample_airbnb database. If you have the sample dataset on your cluster, you can create a custom MongoDB Search index for geo type and run the example queries on your cluster.

次のサンプル インデックス定義を使用して、 listingsAndReviewsコレクションのaddress.locationフィールドにインデックスを作成します。

1{
2 "mappings": {
3 "fields": {
4 "address": {
5 "fields": {
6 "location": {
7 "type": "geo"
8 }
9 },
10 "type": "document"
11 },
12 "property_type": {
13 "type": "token"
14 }
15 }
16 }
17}

次のクエリでは、 geoWithin演算子とboxフィールドを使用して、オーストラリアの境界ボックス内のプロパティを検索します。

クエリには以下が含まれます。

  • $limit stage to limit the output to 3 results.

  • $project stage to exclude all fields except name and address.

注意

MongoDB Search クエリに default という名前のインデックスを指定する必要はありません。インデックスに他の名前がある場合は、indexフィールドを指定する必要があります。

次のクエリでは、 geoWithin演算子とcircleフィールドを使用して、カナダ内の指定された座標の半径 1 マイル以内のプロパティを検索します。

クエリには以下が含まれます。

  • $limit stage to limit the output to 3 results.

  • $project stage to exclude all fields except name and address, and for Compass examples, the required steps reference.

注意

MongoDB Search クエリに default という名前のインデックスを指定する必要はありません。インデックスに他の名前がある場合は、indexフィールドを指定する必要があります。

The following examples use the geoWithin operator with the geometry field to search for properties in Hawaii. The type field specifies whether the area is a GeoJSON Polygon or MultiPolygon.

注意

MongoDB Search クエリに default という名前のインデックスを指定する必要はありません。インデックスに他の名前がある場合は、indexフィールドを指定する必要があります。