AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

$nearSphere (쿼리 자 연산자)

$nearSphere

Specifies a point for which a geospatial query returns the documents from nearest to farthest. MongoDB calculates distances for $nearSphere using spherical geometry.

$nearSphere 지리적 공간 인덱스가 필요합니다.

  • GeoJSON 포인트로 정의된 위치 데이터에 대한 2D 구 인덱스입니다.

  • 레거시 좌표 쌍으로 정의된 위치 데이터에 대한 2D 인덱스입니다. GeoJSON 포인트에 2d 인덱스를 사용하려면 GeoJSON 객체의 coordinates 필드에 인덱스를 생성합니다.

The $nearSphere operator can specify either a GeoJSON point or legacy coordinate point.

GeoJSON 포인트를 지정하려면 다음 구문을 사용합니다.

{
$nearSphere: {
$geometry: {
type : "Point",
coordinates : [ <longitude>, <latitude> ]
},
$minDistance: <distance in meters>,
$maxDistance: <distance in meters>
}
}
  • 선택 사항인 $minDistance는 중심점에서 최소 지정된 거리에 있는 문서로 결과를 제한합니다.

  • 각 인덱스에 대해 선택 사항인 $maxDistance를 사용할 수 있습니다.

기존 좌표를 사용하여 포인트를 지정하려면 다음 구문을 사용합니다.

{
$nearSphere: [ <x>, <y> ],
$minDistance: <distance in radians>,
$maxDistance: <distance in radians>
}
  • 선택 사항인 $minDistance는 쿼리가 2D 구 인덱스를 사용하는 경우에만 사용할 수 있습니다. $minDistance는 중심점에서 최소 지정된 거리에 있는 문서로 결과를 제한합니다.

  • 각 인덱스에 대해 선택 사항인 $maxDistance를 사용할 수 있습니다.

레거시 좌표에 경도와 위도를 사용하는 경우 경도를 먼저 지정한 다음 위도를 지정합니다.

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

The $nearSphere operator sorts documents by distance.

  • 쿼리에 sort() 메서드를 사용하는 경우 MongoDB는 두 번째 정렬 작업을 수행하여 일치하는 문서의 순서를 다시 지정합니다. 대규모 컬렉션을 쿼리할 경우 쿼리 성능에 부정적인 영향을 미칠 수 있습니다.

  • 문서의 순서가 중요하지 않은 경우, 정렬되지 않은 결과를 반환하는 $geoWithin 연산자를 대신 사용하는 것이 좋습니다.

  • $nearSphere 일치 실행 연산자이며 집계 파이프라인에서는 허용되지 않습니다.

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 $nearSphere 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을(를) 지정하여 검색 영역을 제한합니다. 이 메모리는 작업별 메모리 제한에 포함됩니다. 자세한 내용은 작업별 메모리 제한을 참조하세요.

location 필드가 있는 문서를 고 포함하2D 구 인덱스가 있는 컬렉션 places를 가정해 보겠습니다.

그런 다음 다음 예시에서는 지정된 지점에서 최소 1000미터에서 최대 5000미터 떨어진 location을 가장 가까운 곳에서 가장 먼 곳 순으로 반환합니다.

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

location 필드에 레거시 좌표 쌍이 있는 문서를 포함하고 2D 인덱스가 있는 컬렉션 legacyPlaces 경우를 가정해 보겠습니다.

그런 다음 다음 예시에서는 지정된 지점에서 location이 최대 0.10 라디안인 문서를 가장 가까운 것부터 가장 먼 것까지 순서대로 반환합니다.

db.legacyPlaces.find(
{ location : { $nearSphere : [ -73.9667, 40.78 ], $maxDistance: 0.10 } }
)

컬렉션에 대신 2dsphere 인덱스가 있는 경우 선택 사항인 $minDistance 사양을 지정할 수도 있습니다. 예를 들어, 다음 예에서는 location이 지정된 지점으로부터 최소 0.0004 라디안인 문서를 가장 가까운 것부터 가장 먼 것 순으로 반환합니다.

db.legacyPlaces.find(
{ location : { $nearSphere : [ -73.9667, 40.78 ], $minDistance: 0.0004 } }
)
이 페이지 평가하기

이 페이지의 내용