참고
MongoDB Search embeddedDocuments 유형, embeddedDocument 연산자 및 점수 옵션이 미리 보기로 embedded 제공됩니다.
embeddedDocument 연산자 제한 사항
embeddedDocument 연산자 내의 쿼리는 강조 표시할 수 없습니다.
참고
MongoDB Search는 복제본 세트 또는 단일 샤드 에서 파티션당 2,100,000,000 인덱스 객체보다 큰 인덱스에 대한 변경 사항 복제를 중지하며, 여기서 인덱싱된 각 상위 문서 단일 객체 로 계산됩니다. 이 제한을 초과하면 오래된 쿼리 결과가 발생할 수 있습니다.
embeddedDocuments 필드 유형을 사용하면 인덱싱된 각 내장된 문서 단일 객체 로 계산되므로 객체 인덱싱 이 이 인덱스 크기 제한을 초과할 수 있습니다. 인덱스 객체가 {7,700}억 개를 초과하거나 앞으로 갖게 될 MongoDB Search 인덱스 생성하는 경우,2.1 numPartitions 인덱스 옵션을 사용하여 인덱스 분할하거나(검색 노드 배포에서만 지원됨) 클러스터 샤드 .
정의
embeddedDocumentembeddedDocument연산자는 $elemMatch 연산자와 유사합니다. 이 연산자는 여러 쿼리 조건자가 내장된 문서 배열의 단일 요소에서 충족되도록 제한합니다.embeddedDocument는 embeddedDocuments 유형의 필드에 대한 쿼리에만 사용할 수 있습니다.
구문
embeddedDocument 의 구문은 다음과 같습니다:
{ "embeddedDocument": { "path": "<path-to-field>", "operator": { <operator-specification> }, "score": { <score-options> } } }
옵션
embeddedDocument 은(는) 다음 옵션을 사용하여 쿼리를 구성합니다:
필드 | 유형 | 설명 | 필요성 |
|---|---|---|---|
| 객체 |
| 필수 사항 |
| 문자열 | 검색할 인덱싱된 EmbeddedDocuments 유형 필드입니다. 지정된 필드는 | 필수 사항 |
| 객체 | 일치하는 검색 결과에 할당할 점수입니다. 점수 산정 옵션을 사용하여 점수 산정 옵션을 구성할 수 있습니다. | 옵션 |
행동
embeddedDocument 연산자 사용하여 배열에 내장된 문서 쿼리 MongoDB Search는 쿼리 실행의 여러 단계에서 연산자 쿼리 조건자를 평가하고 점수를 매깁니다. MongoDB 검색:
채점 동작
기본적으로 embeddedDocument 연산자는 기본 집계 전략인 sum 를 사용하여 내장된 문서 일치 점수를 결합합니다. embeddedDocument 연산자 score 옵션을 사용하면 기본값을 재정의하고 embedded옵션을 사용하여 일치하는 결과의 점수를 구성할 수 있습니다.
정렬 동작
내장된 문서 필드를 기준으로 상위 문서를 정렬하려면 다음을 수행해야 합니다.
내장된 문서 하위 필드의 상위 항목을 문서 유형으로 인덱싱합니다.
내장된 문서 내에서 문자열 값을 사용하여 하위 필드를 토큰 유형으로 인덱싱합니다. 숫자 및 날짜 값이 있는 하위 필드의 경우 동적 매핑을 활성화하여 해당 필드를 자동으로 인덱싱합니다.
MongoDB Search는 상위 문서에서만 정렬합니다. 문서 배열 내의 하위 필드는 정렬하지 않습니다. 예시 는 정렬 예제.를 참조하세요.
하이라이팅
embeddedDocument 연산자 안에 지정된 쿼리 술어에 대한 문서 유형의 상위 필드에서 필드가 인덱싱되는 경우 필드에서 강조 표시할 강조 표시할 수 있습니다. 예를 들어 튜토리얼을 참조하세요.
연산자 제한에 대해 embeddedDocument embeddedDocument 학습 연산자 제한을 참조하세요.
예시
다음 예시에서는 샘플 데이터 세트의 sample_supplies.sales collection을 사용합니다.
인덱스 정의
이 샘플 쿼리에서는 컬렉션에 대해 다음 인덱스 정의를 사용합니다.
{ "mappings": { "dynamic": true, "fields": { "items": [ { "dynamic": true, "type": "embeddedDocuments" }, { "dynamic": true, "fields": { "tags": { "type": "token" } }, "type": "document" } ], "purchaseMethod": { "type": "token" } } } }
기본 쿼리
다음 쿼리 컬렉션 에서 backpack이라는 항목을 선호하는 school 태그가 지정된 항목을 검색합니다. MongoDB Search는 일치하는 모든 내장된 문서의 평균(산술 MEAN) 점수를 기준으로 내림차순으로 결과의 점수를 매깁니다. 쿼리에는 출력을 5 개의 문서로 제한하는 $limit 단계와 $project 단계가 포함되어 있습니다.
items.name및items.tags필드를 제외한 모든 필드 제외다음과 같은 이름의 필드를 추가합니다.
score
1 db.sales.aggregate({ 2 "$search": { 3 "embeddedDocument": { 4 "path": "items", 5 "operator": { 6 "compound": { 7 "must": [{ 8 "text": { 9 "path": "items.tags", 10 "query": "school" 11 } 12 }], 13 "should": [{ 14 "text": { 15 "path": "items.name", 16 "query": "backpack" 17 } 18 }] 19 } 20 }, 21 "score": { 22 "embedded": { 23 "aggregate": "mean" 24 } 25 } 26 } 27 } 28 }, 29 { 30 $limit: 5 31 }, 32 { 33 $project: { 34 "_id": 0, 35 "items.name": 1, 36 "items.tags": 1, 37 "score": { $meta: "searchScore" } 38 } 39 })
[ { items: [ { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] } ], score: 1.2907354831695557 }, { items: [ { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] }, { name: 'printer paper', tags: [ 'office', 'stationary' ] }, { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] } ], score: 1.2907354831695557 }, { items: [ { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] } ], score: 1.2907354831695557 }, { items: [ { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] } ], score: 1.2907354831695557 }, { items: [ { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] } ], score: 1.2907354831695557 } ]
패싯 쿼리
다음 쿼리는 backpack이라는 항목을 선호하는 school 태그가 지정된 항목을 검색합니다. purchaseMethod 필드에 대한 패싯 정보를 요청합니다.
1 db.sales.aggregate({ 2 "$searchMeta": { 3 "facet": { 4 "operator": { 5 "embeddedDocument": { 6 "path": "items", 7 "operator": { 8 "compound": { 9 "must": [ 10 { 11 "text": { 12 "path": "items.tags", 13 "query": "school" 14 } 15 } 16 ], 17 "should": [ 18 { 19 "text": { 20 "path": "items.name", 21 "query": "backpack" 22 } 23 } 24 ] 25 } 26 } 27 } 28 }, 29 "facets": { 30 "purchaseMethodFacet": { 31 "type": "string", 32 "path": "purchaseMethod" 33 } 34 } 35 } 36 } 37 })
[ { count: { lowerBound: Long("2309") }, facet: { purchaseMethodFacet: { buckets: [ { _id: 'In store', count: Long("2751") }, { _id: 'Online', count: Long("1535") }, { _id: 'Phone', count: Long("578") } ] } } } ]
쿼리 및 정렬
다음 쿼리는 laptop이라는 항목을 검색하고 items.tags 필드를 기준으로 결과를 정렬합니다. 쿼리에는 출력을 5개 문서로 제한하는 $limit 단계와 다음과 같은 $project 단계가 포함됩니다.
items.name및items.tags을 제외한 모든 필드를 제외합니다.다음과 같은 이름의 필드를 추가합니다.
score
1 db.sales.aggregate({ 2 "$search": { 3 "embeddedDocument": { 4 "path": "items", 5 "operator": { 6 "text": { 7 "path": "items.name", 8 "query": "laptop" 9 } 10 } 11 }, 12 "sort": { 13 "items.tags": 1 14 } 15 } 16 }, 17 { 18 "$limit": 5 19 }, 20 { 21 "$project": { 22 "_id": 0, 23 "items.name": 1, 24 "items.tags": 1, 25 "score": { "$meta": "searchScore" } 26 } 27 })
1 [ 2 { 3 items: [ 4 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] }, 5 { name: 'binder', tags: [ 'school', 'general', 'organization' ] }, 6 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 7 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] }, 8 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 9 { name: 'printer paper', tags: [ 'office', 'stationary' ] }, 10 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] }, 11 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 12 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] } 13 ], 14 score: 1.168686032295227 15 }, 16 { 17 items: [ 18 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 19 { name: 'binder', tags: [ 'school', 'general', 'organization' ] }, 20 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 21 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 22 { name: 'printer paper', tags: [ 'office', 'stationary' ] }, 23 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 24 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 25 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] }, 26 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] } 27 ], 28 score: 1.168686032295227 29 }, 30 { 31 items: [ 32 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] }, 33 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 34 { name: 'binder', tags: [ 'school', 'general', 'organization' ] }, 35 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 36 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 37 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] }, 38 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] } 39 ], 40 score: 1.168686032295227 41 }, 42 { 43 items: [ 44 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] }, 45 { name: 'binder', tags: [ 'school', 'general', 'organization' ] }, 46 { name: 'binder', tags: [ 'school', 'general', 'organization' ] }, 47 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] }, 48 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 49 { name: 'printer paper', tags: [ 'office', 'stationary' ] }, 50 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 51 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 52 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 53 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] } 54 ], 55 score: 1.168686032295227 56 }, 57 { 58 items: [ 59 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] }, 60 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 61 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 62 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] }, 63 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] }, 64 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 65 { name: 'binder', tags: [ 'school', 'general', 'organization' ] }, 66 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] }, 67 { name: 'printer paper', tags: [ 'office', 'stationary' ] }, 68 { name: 'binder', tags: [ 'school', 'general', 'organization' ] } 69 ], 70 score: 1.168686032295227 71 } 72 ]
내장된 문서만을 일치시켜 쿼리
다음 쿼리 쿼리 와 일치하는 중첩 문서만 반환합니다. 이 쿼리 단계에서 MongoDB Search 복합 연산자 절을 $search 사용하여 일치하는 문서를 찾은 다음 단계에서 집계 $project 연산자를 사용하여 일치하는 내장된 문서만 반환합니다. 구체적으로 이 쿼리 다음과 같은 파이프라인 단계를 지정합니다.
복합 연산자
| |
출력을 | |
1 db.sales.aggregate( 2 { 3 "$search": { 4 "embeddedDocument": { 5 "path": "items", 6 "operator": { 7 "compound": { 8 "must": [ 9 { 10 "range": { 11 "path": "items.quantity", 12 "gt": 2 13 } 14 }, 15 { 16 "exists": { 17 "path": "items.price" 18 } 19 }, 20 { 21 "text": { 22 "path": "items.tags", 23 "query": "school" 24 } 25 } 26 ] 27 } 28 } 29 } 30 } 31 }, 32 { 33 "$limit": 2 34 }, 35 { 36 "$project": { 37 "_id": 0, 38 "storeLocation": 1, 39 "items": { 40 "$filter": { 41 "input": "$items", 42 "cond": { 43 "$and": [ 44 { 45 "$ifNull": [ 46 "$$this.price", "false" 47 ] 48 }, 49 { 50 "$gt": [ 51 "$$this.quantity", 2 52 ] 53 }, 54 { 55 "$in": [ 56 "office", "$$this.tags" 57 ] 58 } 59 ] 60 } 61 } 62 } 63 } 64 } 65 )
1 [ 2 { 3 storeLocation: 'Austin', 4 items: [ 5 { 6 name: 'laptop', 7 tags: [ 'electronics', 'school', 'office' ], 8 price: Decimal128('753.04'), 9 quantity: 3 10 }, 11 { 12 name: 'pens', 13 tags: [ 'writing', 'office', 'school', 'stationary' ], 14 price: Decimal128('19.09'), 15 quantity: 4 16 }, 17 { 18 name: 'notepad', 19 tags: [ 'office', 'writing', 'school' ], 20 price: Decimal128('30.23'), 21 quantity: 5 22 }, 23 { 24 name: 'pens', 25 tags: [ 'writing', 'office', 'school', 'stationary' ], 26 price: Decimal128('20.05'), 27 quantity: 4 28 }, 29 { 30 name: 'notepad', 31 tags: [ 'office', 'writing', 'school' ], 32 price: Decimal128('22.08'), 33 quantity: 3 34 }, 35 { 36 name: 'notepad', 37 tags: [ 'office', 'writing', 'school' ], 38 price: Decimal128('21.67'), 39 quantity: 4 40 } 41 ] 42 }, 43 { 44 storeLocation: 'Austin', 45 items: [ 46 { 47 name: 'notepad', 48 tags: [ 'office', 'writing', 'school' ], 49 price: Decimal128('24.16'), 50 quantity: 5 51 }, 52 { 53 name: 'notepad', 54 tags: [ 'office', 'writing', 'school' ], 55 price: Decimal128('28.04'), 56 quantity: 5 57 }, 58 { 59 name: 'notepad', 60 tags: [ 'office', 'writing', 'school' ], 61 price: Decimal128('21.42'), 62 quantity: 5 63 }, 64 { 65 name: 'laptop', 66 tags: [ 'electronics', 'school', 'office' ], 67 price: Decimal128('1540.63'), 68 quantity: 3 69 }, 70 { 71 name: 'pens', 72 tags: [ 'writing', 'office', 'school', 'stationary' ], 73 price: Decimal128('29.43'), 74 quantity: 5 75 }, 76 { 77 name: 'pens', 78 tags: [ 'writing', 'office', 'school', 'stationary' ], 79 price: Decimal128('28.48'), 80 quantity: 5 81 } 82 ] 83 } 84 ]
자세히 알아보기
자세한 학습은 내장된 문서의 필드에 대해 MongoDB Search 쿼리를 실행하는 방법을 참조하세요.