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

$near(クエリ述語演算子)

$near

Specifies a point for which a geospatial query returns the documents from nearest to farthest. The $near operator can specify either a GeoJSON point or legacy coordinate point.

$near には地理空間インデックスが必要です。

To specify a GeoJSON point, $near operator requires a 2dsphere index and has the following syntax:

{
<location field>: {
$near: {
$geometry: {
type: "Point" ,
coordinates: [ <longitude> , <latitude> ]
},
$maxDistance: <distance in meters>,
$minDistance: <distance in meters>
}
}
}

重要

緯度と経度の座標を指定する場合は、最初に経度、次に緯度を指定します。

  • 有効な経度の値は、-180 以上、180 以下です。

  • 有効な緯度の値は-90 以上、90 以下です。

When specifying a GeoJSON point, you can use the optional $minDistance and $maxDistance specifications to limit the $near results by distance in meters:

  • $minDistance では、中心点から指定された以上の距離にあるドキュメントに結果が制限されます。

  • $maxDistance では、中心点から指定された以内の距離にあるドキュメントに結果が制限されます。

To specify a point using legacy coordinates, $near requires a 2d index and has the following syntax:

{
$near: [ <x>, <y> ],
$maxDistance: <distance in radians>
}

When specifying a legacy coordinate, you can use the optional $maxDistance specification to limit the $near results by distance in radians. $maxDistance limits the results to those documents that are at most the specified distance from the center point.

You cannot combine the $near operator, which requires a special geospatial index, with a query operator or command that requires another special index. For example you cannot combine $near with the $text query.

The $near operator sorts documents by distance.

  • クエリで sort() メソッドを使用すると、MongoDB は 2 番目の並べ替え操作を実行して、一致するドキュメントの順序を変更します。大規模なコレクションをクエリする場合、これはクエリのパフォーマンスに悪影響を与える可能性があります。

  • ドキュメントの順序が重要でない場合は、並べ替えられていない結果が返されるため、代わりに $geoWithin 演算子の使用を検討してください。

  • $near はマッチ式演算子であり、集計パイプラインでは許可されていません。

Starting in MongoDB 8.0, $near, $nearSphere, and $geoNear validate that the type of the specified GeoJSON points is Point. Any other input type returns an error.

Starting in MongoDB 9.0, MongoDB tracks the memory that $near uses to buffer results and to remove duplicate records. If the query exceeds the 100 megabyte memory limit for the stage, MongoDB ends the query and returns an error.

多数のドキュメントに一致するクエリのみが上限に達します。クエリに必要なメモリを削減するには、検索範囲を制限するために maxDistance を指定します。このメモリは、操作ごとのメモリ制限に含まれます。さらに学ぶには、「操作ごとのメモリ制限」を参照してください。

重要

緯度と経度の座標を指定する場合は、最初に経度、次に緯度を指定します。

  • 有効な経度の値は、-180 以上、180 以下です。

  • 有効な緯度の値は-90 以上、90 以下です。

2dsphere インデックスのあるコレクション places を考えてみましょう。

次の例では、指定された GeoJSON ポイントから少なくとも 1000 メートル、最大 5000 メートル離れたドキュメントを、近いものから遠いものの順に返します。

db.places.find(
{
location:
{ $near :
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)

重要

緯度と経度の座標を指定する場合は、最初に経度、次に緯度を指定します。

  • 有効な経度の値は、-180 以上、180 以下です。

  • 有効な緯度の値は-90 以上、90 以下です。

2d インデックスのあるコレクション legacy2d を考えてみましょう。

次の例では、指定されたレガシー座標ペアから最大 0.10 ラジアンのドキュメントを、最も近いものから最も遠いものの順にソートして返します。

db.legacy2d.find(
{ location : { $near : [ -73.9667, 40.78 ], $maxDistance: 0.10 } }
)
このページを評価

項目一覧