문서 메뉴

문서 홈MongoDB 실행 및 관리MongoDB Atlas

geoShape

이 페이지의 내용

  • 정의
  • 구문
  • 옵션
  • 예제
geoShape

인덱스 정의에서 indexShapestrue 로 설정된 경우 geoShape 연산자는 지정된 도형과의 관계가 있는 도형 쿼리를 지원합니다.

검색할 좌표를 지정할 때는 경도를 먼저 지정한 다음 위도를 지정해야 합니다. 경도 값 범위는 -180 ~ 180 사이이며 둘 다 포함합니다. 위도 값 범위는 -90 ~ 90 이며 둘 다 포함합니다. 좌표 값은 정수 또는 이중 값일 수 있습니다.

참고

Atlas Search에서는 다음을 지원하지 않습니다.

  • 기본값이 좌표 기준계(CRS)

  • 평면형 XY 좌표계(2차원)

  • 좌표 쌍 점 표기법(즉, pointFieldName: [12, 34])

1{
2 "$search": {
3 "index": <index name>, // optional, defaults to "default"
4 "geoShape": {
5 "path": "<field-to-search>",
6 "relation": "contains | disjoint | intersects | within",
7 "geometry": <GeoJSON-object>,
8 "score": <score-options>
9 }
10 }
11}

geoShape 는 다음 용어를 사용하여 쿼리를 구성합니다:

필드
유형
설명
필요성
geometry
GeoJSON 객체

검색할 다각형, 다중 다각형 또는 라인스트링 모양이나 점을 지정하는 GeoJSON 객체입니다. 다각형은 마지막 위치가 첫 번째 위치와 동일한 닫힌 루프로 지정되어야 합니다.

지리 공간적 결과를 계산할 때 Atlas Search geoShapegeoWithin 연산자와 MongoDB $geoIntersects 연산자는 서로 다른 도형을 사용합니다. 이 차이점은 Atlas Search와 MongoDB가 다각형 가장자리를 그리는 방식에서 확인할 수 있습니다.

Atlas Search는 데카르트 거리 를 기반으로 다각형을 그립니다. 은 좌표 참조 시스템에서 두 점을 잇는 가장 짧은 선입니다.

MongoDB는 측지 유형 에 타사 라이브러리를 사용하여 다각형을 그립니다. 측지선을 사용합니다. 자세한 내용은 GeoJSON 객체를 참조하세요.

Atlas Search와 MongoDB는 다각형과 관련된 지리 공간적 쿼리에 대해 서로 다른 결과를 반환할 수 있습니다.

path
문자열 또는 문자열 배열
인덱싱된 지역 유형에서 검색할 필드를 하나 이상 입력합니다. 자세한 내용은 경로 구성 을 참조하세요.
relation
열거형

쿼리 형태 도형과 인덱싱된 필드 도형의 관계입니다. 값은 다음 중 하나일 수 있습니다.

  • contains - 인덱싱된 도형에 쿼리 도형이 포함되어 있음을 나타냅니다.

  • disjoint - 쿼리와 인덱싱된 도형 모두에 공통점이 없음을 나타냅니다.

  • intersects - 쿼리와 인덱싱된 도형이 모두 교차함을 나타냅니다.

  • within - 인덱싱된 도형이 쿼리 도형 내에 있음을 나타냅니다. withinLineString 또는 Point 와 함께 사용할 수 없습니다.

score
객체

일치하는 검색 결과에 할당할 점수입니다. 기본적으로 결과의 점수1 입니다. 다음 옵션을 사용하여 점수를 수정할 수 있습니다.

  • boost결과 점수에 주어진 숫자를 곱합니다.

  • constant결과 점수를 주어진 숫자로 바꿉니다.

  • function결과 점수를 주어진 표현식으로 대체합니다.

쿼리에서 score 사용에 대한 자세한 내용은 결과에서 문서 채점하기를 참조하십시오.

아니

다음 예제에서는 sample_airbnb 데이터베이스의 listingsAndReviews 컬렉션을 사용합니다. 클러스터에 샘플 데이터 세트 가 있는 경우 지리적 유형에 대한 사용자 지정 Atlas Search 인덱스를 만들고 클러스터에서 예제 쿼리를 실행할 수 있습니다.

샘플 데이터 집합을 이미 로드한 경우, Atlas Search 시작하기 자습서에 따라 인덱스 정의를 만들고 Atlas Search 쿼리를 실행하세요.

다음은 listingsAndReviews collection의 address.location 필드를 인덱싱하기 위한 샘플 인덱스 정의입니다.

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

Atlas Search 시작하기에는 샘플 데이터 세트 로드, 인덱스 정의 생성 및 Atlas Search 검색 쿼리 실행에 대한 지침이 포함되어 있습니다.

참고

다음 샘플 쿼리의 경우 인덱스 정의에서 indexShapestrue 로 설정되어 있는지 확인합니다.

다음 예에서는 geoShape 연산자를 사용하여 하와이의 지정된 경도 및 위도 좌표와 공통점이 없는 속성을 검색합니다.

쿼리에는 다음이 포함됩니다.

  • $limit 단계를 사용하여 출력을 3 결과로 제한합니다.

  • $project 단계에서 nameaddress 제외한 모든 필드를 제외합니다.

1db.listingsAndReviews.aggregate([
2 {
3 "$search": {
4 "geoShape": {
5 "relation": "disjoint",
6 "geometry": {
7 "type": "Polygon",
8 "coordinates": [[[-161.323242,22.512557],
9 [-152.446289,22.065278],
10 [-156.09375,17.811456],
11 [-161.323242,22.512557]]]
12 },
13 "path": "address.location"
14 }
15 }
16 },
17 {
18 $limit: 3
19 },
20 {
21 $project: {
22 "_id": 0,
23 "name": 1,
24 "address": 1,
25 score: { $meta: "searchScore" }
26 }
27 }
28])

쿼리는 다음과 같은 검색 결과를 반환합니다.

1{
2 "name" : "Ribeira Charming Duplex",
3 "address" : {
4 "street" : "Porto, Porto, Portugal",
5 "suburb" : "",
6 "government_area" : "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória",
7 "market" : "Porto",
8 "country" : "Portugal",
9 "country_code" : "PT",
10 "location" : {
11 "type" : "Point",
12 "coordinates" : [ -8.61308, 41.1413 ],
13 "is_location_exact" : false
14 }
15 }
16}
17{
18 "name" : "Horto flat with small garden",
19 "address" : {
20 "street" : "Rio de Janeiro, Rio de Janeiro, Brazil",
21 "suburb" : "Jardim Botânico",
22 "government_area" : "Jardim Botânico",
23 "market" : "Rio De Janeiro",
24 "country" : "Brazil",
25 "country_code" : "BR",
26 "location" : {
27 "type" : "Point",
28 "coordinates" : [ -43.23074991429229, -22.966253551739655 ],
29 "is_location_exact" : true
30 }
31 }
32}
33{
34 "name" : "Private Room in Bushwick",
35 "address" : {
36 "street" : "Brooklyn, NY, United States",
37 "suburb" : "Brooklyn",
38 "government_area" : "Bushwick",
39 "market" : "New York",
40 "country" : "United States",
41 "country_code" : "US",
42 "location" : {
43 "type" : "Point",
44 "coordinates" : [ -73.93615, 40.69791 ],
45 "is_location_exact" : true
46 }
47 }
48}

다음 예제에서는 geoShape 연산자를 사용하여 스페인에서 지정된 경도 및 위도 좌표와 교차하는 속성을 검색합니다.

쿼리에는 다음이 포함됩니다.

  • $limit 단계를 사용하여 출력을 3 결과로 제한합니다.

  • $project 단계에서 nameaddress 제외한 모든 필드를 제외합니다.

1db.listingsAndReviews.aggregate([
2 {
3 "$search": {
4 "geoShape": {
5 "relation": "intersects",
6 "geometry": {
7 "type": "MultiPolygon",
8 "coordinates": [
9 [[[2.16942,41.40082],
10 [2.17963,41.40087],
11 [2.18146,41.39716],
12 [2.15533,41.40686],
13 [2.14596,41.38475],
14 [2.17519,41.41035],
15 [2.16942,41.40082]]],
16 [[[2.16365,41.39416],
17 [2.16963,41.39726],
18 [2.15395,41.38005],
19 [2.17935,41.43038],
20 [2.16365,41.39416]]]
21 ]
22 },
23 "path": "address.location"
24 }
25 }
26 },
27 {
28 $limit: 3
29 },
30 {
31 $project: {
32 "_id": 0,
33 "name": 1,
34 "address": 1,
35 score: { $meta: "searchScore" }
36 }
37 }
38])

쿼리는 다음과 같은 검색 결과를 반환합니다.

1{
2 "name" : "Cozy bedroom Sagrada Familia",
3 "address" : {
4 "street" : "Barcelona, Catalunya, Spain",
5 "suburb" : "Eixample",
6 "government_area" : "el Fort Pienc",
7 "market" : "Barcelona",
8 "country" : "Spain",
9 "country_code" : "ES",
10 "location" : {
11 "type" : "Point",
12 "coordinates" : [ 2.17963, 41.40087 ],
13 "is_location_exact" : true
14 }
15 }
16}
17{
18 "name" : "",
19 "address" : {
20 "street" : "Barcelona, Catalunya, Spain",
21 "suburb" : "Vila de Gràcia",
22 "government_area" : "la Vila de Gràcia",
23 "market" : "Barcelona",
24 "country" : "Spain",
25 "country_code" : "ES",
26 "location" : {
27 "type" : "Point",
28 "coordinates" : [ 2.15759, 41.40349 ],
29 "is_location_exact" : true
30 }
31 }
32}
33{
34 "name" : "SPACIOUS RAMBLA CATALUÑA",
35 "address" : {
36 "street" : "Barcelona, Catalunya, Spain",
37 "suburb" : "L'Antiga Esquerra de l'Eixample",
38 "government_area" : "l'Antiga Esquerra de l'Eixample",
39 "market" : "Barcelona",
40 "country" : "Spain",
41 "country_code" : "ES",
42 "location" : {
43 "type" : "Point",
44 "coordinates" : [ 2.15255, 41.39193 ],
45 "is_location_exact" : true
46 }
47 }
48}

다음 예제에서는 geoShape 연산자를 사용하여 지정된 경도 및 위도 좌표 내에 있는 뉴욕의 속성을 검색합니다. 쿼리는 sample_airbnb 데이터베이스의 listingsAndReviews collection에 있는 address.location 필드를 검색합니다.

쿼리에는 다음이 포함됩니다.

  • $limit 단계를 사용하여 출력을 3 결과로 제한합니다.

  • $project 단계에서 nameaddress 제외한 모든 필드를 제외합니다.

1db.listingsAndReviews.aggregate([
2 {
3 "$search": {
4 "geoShape": {
5 "relation": "within",
6 "geometry": {
7 "type": "Polygon",
8 "coordinates": [[[-74.3994140625,40.5305017757],
9 [-74.7290039063,40.5805846641],
10 [-74.7729492188,40.9467136651],
11 [-74.0698242188,41.1290213475],
12 [-73.65234375,40.9964840144],
13 [-72.6416015625,40.9467136651],
14 [-72.3559570313,40.7971774152],
15 [-74.3994140625,40.5305017757]]]
16 },
17 "path": "address.location"
18 }
19 }
20 },
21 {
22 $limit: 3
23 },
24 {
25 $project: {
26 "_id": 0,
27 "name": 1,
28 "address": 1,
29 score: { $meta: "searchScore" }
30 }
31 }
32])

쿼리는 다음과 같은 검색 결과를 반환합니다.

1{
2 "name" : "Private Room in Bushwick",
3 "address" : {
4 "street" : "Brooklyn, NY, United States",
5 "suburb" : "Brooklyn",
6 "government_area" : "Bushwick",
7 "market" : "New York",
8 "country" : "United States",
9 "country_code" : "US",
10 "location" : {
11 "type" : "Point",
12 "coordinates" : [ -73.93615, 40.69791 ],
13 "is_location_exact" : true
14 }
15 },
16 {
17 "name" : "New York City - Upper West Side Apt",
18 "address" : {
19 "street" : "New York, NY, United States",
20 "suburb" : "Manhattan",
21 "government_area" : "Upper West Side",
22 "market" : "New York",
23 "country" : "United States",
24 "country_code" : "US",
25 "location" : {
26 "type" : "Point",
27 "coordinates" : [ -73.96523, 40.79962 ],
28 "is_location_exact" : false
29 }
30 },
31 "score" : 1
32 }
33 {
34 "name" : "Deluxe Loft Suite",
35 "address" : {
36 "street" : "Brooklyn, NY, United States",
37 "suburb" : "Greenpoint",
38 "government_area" : "Greenpoint",
39 "market" : "New York",
40 "country" : "United States",
41 "country_code" : "US",
42 "location" : {
43 "type" : "Point",
44 "coordinates" : [ -73.94472, 40.72778 ],
45 "is_location_exact" : true
46 }
47 },
48 "score" : 1
49 }
← 패싯

이 페이지의 내용