정의
geoShape
geoShape
연산자는 인덱스 정의에서indexShapes
가true
로 설정된 경우 지정된 지오메트리와 관련된 형태를 쿼리하는 것을 지원합니다.검색할 좌표를 지정할 때는 경도를 먼저 지정한 다음 위도를 지정해야 합니다. 경도 값 범위는
-180
~180
사이이며 둘 다 포함합니다. 위도 값 범위는-90
~90
이며 둘 다 포함합니다. 좌표 값은 정수 또는 이중 값일 수 있습니다.참고
MongoDB 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
는 다음 용어를 사용하여 쿼리를 구성합니다:
필드 | 유형 | 설명 | 필요성 |
---|---|---|---|
| GeoJSON 객체 | 다각형 , 다중 다각형 또는GeoJSON 라인스트링 모양 또는 지점을 지정하는 객체로, Atlas Search를 가리킵니다. 다각형은 마지막 위치가 첫 번째 위치와 동일한 닫힌 루프로 지정되어야 합니다. 지리 공간적 결과를 계산할 때 MongoDB Search geoShape 및 geoWithin 연산자와 MongoDB $geoIntersects 연산자 서로 다른 도형을 사용합니다. 이 차이는 MongoDB Search와 MongoDB 다각형 가장자리를 그리는 방법에서 확인할 수 있습니다. MongoDB Search는 좌표 참조 시스템의 두 점 사이의 가장 짧은 선인 데카르트 거리 를 기준으로 다각형을 그립니다. MongoDB 측지 유형용 타사 라이브러리를 기반으로 하는 2dsphere 인덱스를 기반으로 하는 측지 모드 또는 2d 인덱스의 플랫 모드 를 사용하여 다각형을 그립니다. 자세한 학습 은 GeoJSON 객체를 참조하세요. MongoDB Search와 MongoDB 다각형과 관련된 지리 공간적 쿼리에 대해 서로 다른 결과를 반환할 수 있습니다. | 네 |
| 문자열 또는 문자열 배열 | 검색할 인덱싱된 geo 유형 필드. | 네 |
| 열거형 | 쿼리 형태 도형과 인덱싱된 필드 도형의 관계입니다. 값은 다음 중 하나일 수 있습니다.
| 네 |
| 객체 | 일치하는 검색 결과에 할당할 점수입니다. 기본적으로 결과의 점수 는
쿼리에서 | no |
예시
다음 예제에서는 sample_airbnb
데이터베이스 의 listingsAndReviews
컬렉션 사용합니다. 클러스터에 샘플 데이터 세트 가 있는 경우 지리적 유형에 대한 사용자 지정 MongoDB Search 인덱스를 만들고 클러스터에서 예시 쿼리를 실행할 수 있습니다. MongoDB Search 빠른 시작 에는 샘플 데이터 세트를 로드하고, 인덱스 정의를 만들고, MongoDB 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 "property_type": { 14 "type": "token" 15 } 16 } 17 } 18 }
분리된 예제
다음 예에서는 geoShape
연산자를 사용하여 하와이의 지정된 경도 및 위도 좌표와 공통점이 없는 속성을 검색합니다.
쿼리에는 다음이 포함됩니다.
1 db.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 ])
{ "name" : "Ribeira Charming Duplex", "address" : { "street" : "Porto, Porto, Portugal", "suburb" : "", "government_area" : "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória", "market" : "Porto", "country" : "Portugal", "country_code" : "PT", "location" : { "type" : "Point", "coordinates" : [ -8.61308, 41.1413 ], "is_location_exact" : false } } } { "name" : "Horto flat with small garden", "address" : { "street" : "Rio de Janeiro, Rio de Janeiro, Brazil", "suburb" : "Jardim Botânico", "government_area" : "Jardim Botânico", "market" : "Rio De Janeiro", "country" : "Brazil", "country_code" : "BR", "location" : { "type" : "Point", "coordinates" : [ -43.23074991429229, -22.966253551739655 ], "is_location_exact" : true } } } { "name" : "Private Room in Bushwick", "address" : { "street" : "Brooklyn, NY, United States", "suburb" : "Brooklyn", "government_area" : "Bushwick", "market" : "New York", "country" : "United States", "country_code" : "US", "location" : { "type" : "Point", "coordinates" : [ -73.93615, 40.69791 ], "is_location_exact" : true } } }
교차 예제
다음 예제에서는 geoShape
연산자를 사용하여 스페인에서 지정된 경도 및 위도 좌표와 교차하는 속성을 검색합니다.
쿼리에는 다음이 포함됩니다.
1 db.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 ])
{ "name" : "Cozy bedroom Sagrada Familia", "address" : { "street" : "Barcelona, Catalunya, Spain", "suburb" : "Eixample", "government_area" : "el Fort Pienc", "market" : "Barcelona", "country" : "Spain", "country_code" : "ES", "location" : { "type" : "Point", "coordinates" : [ 2.17963, 41.40087 ], "is_location_exact" : true } } } { "name" : "", "address" : { "street" : "Barcelona, Catalunya, Spain", "suburb" : "Vila de Gràcia", "government_area" : "la Vila de Gràcia", "market" : "Barcelona", "country" : "Spain", "country_code" : "ES", "location" : { "type" : "Point", "coordinates" : [ 2.15759, 41.40349 ], "is_location_exact" : true } } } { "name" : "SPACIOUS RAMBLA CATALUÑA", "address" : { "street" : "Barcelona, Catalunya, Spain", "suburb" : "L'Antiga Esquerra de l'Eixample", "government_area" : "l'Antiga Esquerra de l'Eixample", "market" : "Barcelona", "country" : "Spain", "country_code" : "ES", "location" : { "type" : "Point", "coordinates" : [ 2.15255, 41.39193 ], "is_location_exact" : true } } }
예제 내
다음 예제에서는 geoShape
연산자를 사용하여 지정된 경도 및 위도 좌표 내에 있는 뉴욕의 속성을 검색합니다. 쿼리는 sample_airbnb
데이터베이스의 listingsAndReviews
collection에 있는 address.location
필드를 검색합니다.
쿼리에는 다음이 포함됩니다.
기본 검색
다음 쿼리는 지정된 검색 기준과 일치하는 문서를 반환합니다.
1 db.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 ])
{ "name" : "Private Room in Bushwick", "address" : { "street" : "Brooklyn, NY, United States", "suburb" : "Brooklyn", "government_area" : "Bushwick", "market" : "New York", "country" : "United States", "country_code" : "US", "location" : { "type" : "Point", "coordinates" : [ -73.93615, 40.69791 ], "is_location_exact" : true } }, { "name" : "New York City - Upper West Side Apt", "address" : { "street" : "New York, NY, United States", "suburb" : "Manhattan", "government_area" : "Upper West Side", "market" : "New York", "country" : "United States", "country_code" : "US", "location" : { "type" : "Point", "coordinates" : [ -73.96523, 40.79962 ], "is_location_exact" : false } }, "score" : 1 } { "name" : "Deluxe Loft Suite", "address" : { "street" : "Brooklyn, NY, United States", "suburb" : "Greenpoint", "government_area" : "Greenpoint", "market" : "New York", "country" : "United States", "country_code" : "US", "location" : { "type" : "Point", "coordinates" : [ -73.94472, 40.72778 ], "is_location_exact" : true } }, "score" : 1 }
메타데이터 검색
다음 쿼리는 지정된 검색 조건에 따라 아파트, 주택 등의 부동산 유형 수를 반환합니다.
1 db.listingsAndReviews.aggregate([ 2 { 3 "$searchMeta": { 4 "facet": { 5 "operator": { 6 "geoShape": { 7 "relation": "within", 8 "geometry": { 9 "type": "Polygon", 10 "coordinates": [[[-74.3994140625,40.5305017757], 11 [-74.7290039063,40.5805846641], 12 [-74.7729492188,40.9467136651], 13 [-74.0698242188,41.1290213475], 14 [-73.65234375,40.9964840144], 15 [-72.6416015625,40.9467136651], 16 [-72.3559570313,40.7971774152], 17 [-74.3994140625,40.5305017757]]] 18 }, 19 "path": "address.location" 20 } 21 }, 22 "facets": { 23 "propertyTypeFacet": { 24 "type": "string", 25 "path": "property_type" 26 } 27 } 28 } 29 } 30 } 31 ])
[ { count: { lowerBound: Long('599') }, facet: { propertyTypeFacet: { buckets: [ { _id: 'Apartment', count: Long('486') }, { _id: 'House', count: Long('43') }, { _id: 'Townhouse', count: Long('24') }, { _id: 'Condominium', count: Long('19') }, { _id: 'Loft', count: Long('19') }, { _id: 'Guest suite', count: Long('2') }, { _id: 'Guesthouse', count: Long('2') }, { _id: 'Aparthotel', count: Long('1') }, { _id: 'Hostel', count: Long('1') }, { _id: 'Serviced apartment', count: Long('1') } ] } } } ]
쿼리 결과는 지정된 좌표 내의 다양한 유형의 부동산을 보여줍니다.