定義
$nearSpecifies a point for which a geospatial query returns the documents from nearest to farthest. The
$nearoperator can specify either a GeoJSON point or legacy coordinate point.$nearには地理空間インデックスが必要です。GeoJSON ポイントを指定する場合の 2dsphere インデックス。
レガシー座標を使用して点を指定する場合の 2D インデックス。
To specify a GeoJSON point,
$nearoperator 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
$minDistanceand$maxDistancespecifications to limit the$nearresults by distance in meters:$minDistanceでは、中心点から指定された以上の距離にあるドキュメントに結果が制限されます。$maxDistanceでは、中心点から指定された以内の距離にあるドキュメントに結果が制限されます。
To specify a point using legacy coordinates,
$nearrequires 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
$maxDistancespecification to limit the$nearresults by distance in radians.$maxDistancelimits 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 を指定します。このメモリは、操作ごとのメモリ制限に含まれます。さらに学ぶには、「操作ごとのメモリ制限」を参照してください。
例
GeoJSON データに対するクエリ
重要
緯度と経度の座標を指定する場合は、最初に経度、次に緯度を指定します。
有効な経度の値は、
-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 } } )