returnScope 옵션을 사용하여 쿼리 의 컨텍스트를 설정하다 하고 객체 배열을 개별 문서로 반환할 수 있습니다.
요구 사항:
returnScope를 사용하여 중첩된 객체를 개별 문서로 조회하려면 다음을 수행해야 합니다.
객체 배열을 embeddedDocuments 유형으로 인덱싱합니다.
조회할 중첩 필드에 대해 storedSource를 정의합니다. MongoDB 검색은
storedSource에 정의된 필드만 반환합니다.쿼리에서 returnStoredSource 옵션을
true로 설정합니다.
구문
returnScope 쿼리에 다음 구문이 있습니다.
쿼리 구문에 대해 자세히 알아보려면 $search 를 참조하세요.
행동
returnScope 옵션은 쿼리의 검색 컨텍스트를 설정합니다. 쿼리에 returnScope를 지정하면, MongoDB 검색는 각 내장된 문서를 개별 문서처럼 점수화하고, 정렬하며, 개수를 셉니다.
고려 사항
returnScope 옵션을 사용할 때, MongoDB 검색은 embeddedDocument 내에서 storedSource로 구성된 필드만 반환합니다. embeddedDocument 경로 외부의 필드(예: 루트 수준의 필드) 및 storedSource로 설정되지 않은 필드는 반환되지 않습니다.
연산자 사양에서 쿼리 하려는 필드 의 전체 경로를 지정해야 합니다. returnScope 옵션을 사용하는 경우 모든 연산자 사양 경로가 returnScope.path 아래에 중첩되어 있는지 확인해야 합니다. returnScope.path 외부의 필드 쿼리 하려면 hasAncestor 또는 hasRoot 연산자 사용해야 합니다. 자세한 학습 은 다음을 참조하세요.
루트 문서 ID 조회
쿼리에서 returnScope 을(를) 사용하면 MongoDB Search가 searchRootDocumentId 메타데이터 필드를 채우게 됩니다. MongoDB 8.3 이상을 실행하는 클러스터에서 searchRootDocumentId 메타데이터 필드를 사용할 수 있습니다. 이 필드에는 반환된 각 내장된 문서가 포함된 루트 문서의 식별자가 포함되어 있습니다. 이 값을 프로젝하려면 searchRootDocumentId 키워드와 함께 $meta 표현식을 사용합니다.
MongoDB Search는 쿼리가 returnStoredSource: true 과 returnScope.path를 모두 설정한 경우에만 searchRootDocumentId 을 채우집니다. returnScope를 지정하지 않는 쿼리에서 searchRootDocumentId 를 참조하면 다음 오류가 발생하면서 쿼리가 실패합니다.
query requires $search root document id metadata, but it is not available
다음을 수행하려면 searchRootDocumentId 을 사용합니다.
하위 문서를 필터링한 후 상위 문서에서 필드를 가져올 수 있습니다.
부모를 기준으로 하위 문서를 그룹화합니다.
다음의 경우 searchRootDocumentId 을(를) 사용하지 마십시오.
일치하는 하위 문서의 필드만 필요합니다.
이는 비용이 많이 들 수 있으므로 결과 세트의 모든 하위 문서에 대한 상위 문서를 반환하려고 합니다.
예시
다음 예제는 쿼리에서 returnScope 옵션을 사용하는 방법을 보여 줍니다. 이 예제에서는 sample_training.companies 샘플 데이터세트를 사용합니다. 클러스터에 데이터를 로드하고 컬렉션의 필드에 샘플 인덱스를 생성하면, 샘플 데이터에 대해 다음 쿼리를 실행할 수 있습니다.
샘플 인덱스
1 { 2 "mappings": { 3 "dynamic": false, 4 "fields": { 5 "funding_rounds": { 6 "type": "embeddedDocuments", 7 "dynamic": true, 8 "fields": { 9 "investments": { 10 "type": "embeddedDocuments", 11 "dynamic": true 12 } 13 }, 14 "storedSource": { 15 "include": [ 16 "round_code", 17 "raised_currency_code", 18 "raised_amount", 19 "investments.person", 20 "investments.financial_org" 21 ] 22 } 23 } 24 } 25 } 26 }
위의 인덱스 정의는 MongoDB 검색하다를 다음과 같이 구성합니다.
funding_rounds및funding_rounds.investments필드를embeddedDocuments유형으로 인덱싱합니다.funding_rounds및funding_rounds.investments객체 배열에 중첩된 모든 동적으로 인덱스할 수 있는 필드를 인덱싱합니다.mongot에 다음 필드를 저장합니다.funding_rounds.round_codefunding_rounds.raised_currency_codefunding_rounds.raised_amountfunding_rounds.investments.personfunding_rounds.investments.financial_org
embeddedDocument 연산자를 사용하여 funding_rounds 및 funding_rounds.investments 필드에서 요소별 쿼리를 수행할 수 있습니다. 다음 섹션에서는 returnScope 옵션을 사용하여 embeddedDocuments 필드를 개별 문서로 조회하는 몇 가지 샘플 쿼리를 보여줍니다.
{ ..., "funding_rounds": [ { "id": <integer>, "round_code": "<string>", "source_url": "<string>", "source_description": "<string>", "raised_amount": <integer>, "raised_currency_code": "<string>", "funded_year": <integer>, "funded_month": "<string>", "funded_day": "<string>", "investments": [ { "company": "<string>", "financial_org": { "name": "<string>", "permalink": "<string>" }, "person": { "first_name": "<string>", "last_name": "<string>", "permalink": "<string>" } }, ... ] }, ... ], ... }
샘플 쿼리
다음 섹션에서는 returnScope 옵션을 사용하여 mongot에 저장된 embeddedDocuments 유형 필드에서 필드를 조회하는 샘플 쿼리를 보여줍니다.
다음 range 쿼리(MongoDB 검색 연산자)를 사용하여 funding_rounds.raised_amount 필드 에 이상 및 이하 5000000 및 이하의 금액을 쿼리 10000000. returnScope 옵션을 사용하여 쿼리 범위를 funding_rounds 필드 로 설정합니다. returnStoredSource 옵션을 사용하여 저장된 객체의 funding_rounds.investments 배열 에 있는 필드를 포함하여 객체의 funding_rounds 배열 내에 저장된 모든 필드를 반환합니다. 결과 수를 5 funding_rounds 문서로 제한합니다.
1 db.companies.aggregate( 2 { 3 "$search": { 4 "range": { 5 "path": "funding_rounds.raised_amount", 6 "gte": 5000000, 7 "lte": 10000000 8 }, 9 "returnStoredSource": true, 10 "returnScope": { 11 "path": "funding_rounds" 12 } 13 } 14 }, 15 { 16 "$limit": 5 17 } 18 )
[ { round_code: 'a', raised_amount: 5250000, raised_currency_code: 'USD', investments: [ { financial_org: { name: 'Frazier Technology Ventures', permalink: 'frazier-technology-ventures' }, person: null }, { financial_org: { name: 'Trinity Ventures', permalink: 'trinity-ventures' }, person: null } ] }, { round_code: 'b', raised_amount: 9500000, raised_currency_code: 'USD', investments: [ { financial_org: { name: 'Accel Partners', permalink: 'accel-partners' }, person: null }, { financial_org: { name: 'Frazier Technology Ventures', permalink: 'frazier-technology-ventures' }, person: null }, { financial_org: { name: 'Trinity Ventures', permalink: 'trinity-ventures' }, person: null } ] }, { round_code: 'a', raised_amount: 5000000, raised_currency_code: 'USD', investments: [ { financial_org: { name: 'Charles River Ventures', permalink: 'charles-river-ventures' }, person: null }, { financial_org: { name: 'Union Square Ventures', permalink: 'union-square-ventures' }, person: null }, { financial_org: null, person: { first_name: 'Marc', last_name: 'Andreessen', permalink: 'marc-andreessen' } }, { financial_org: null, person: { first_name: 'Dick', last_name: 'Costolo', permalink: 'dick-costolo' } }, { financial_org: null, person: { first_name: 'Naval', last_name: 'Ravikant', permalink: 'naval-ravikant' } }, { financial_org: null, person: { first_name: 'Ron', last_name: 'Conway', permalink: 'ron-conway' } }, { financial_org: null, person: { first_name: 'Chris', last_name: 'Sacca', permalink: 'chris-sacca' } }, { financial_org: null, person: { first_name: 'Greg', last_name: 'Yaitanes', permalink: 'greg-yaitanes' } }, { financial_org: null, person: { first_name: 'Brian', last_name: 'Pokorny', permalink: 'brian-pokorny' } }, { financial_org: { name: 'SV Angel', permalink: 'sv-angel' }, person: null } ] }, { round_code: 'e', raised_amount: 5166511, raised_currency_code: 'USD', investments: [] }, { round_code: 'b', raised_amount: 9000000, raised_currency_code: 'USD', investments: [ { financial_org: { name: 'Charles River Ventures', permalink: 'charles-river-ventures' }, person: null }, { financial_org: { name: 'Redpoint Ventures', permalink: 'redpoint-ventures' }, person: null }, { financial_org: { name: 'The Kinsey Hills Group', permalink: 'kinsey-hills-group' }, person: null } ] } ]
다음 쿼리는 compound 연산자를 사용하여 동일한 쿼리에서 중첩된 embeddedDocuments 필드의 여러 수준을 검색합니다.
``funding_rounds.raised_currency_code`` 가
USDfunding_rounds.investments.financial_org.name을(를)Trinity Ventures와(과) 일치시켜야 합니다.
funding_rounds.investments에 있는 필드를 포함하여 funding_rounds 객체 배열 내에 저장된 모든 필드를 반환합니다. 결과 수를 funding_rounds개 문서로 제한합니다.
1 db.companies.aggregate( 2 { 3 "$search": { 4 "compound": { 5 "must": [{ 6 "text": { 7 "path": "funding_rounds.raised_currency_code", 8 "query": "usd" 9 } 10 }], 11 "should": [{ 12 "phrase": { 13 "path": "funding_rounds.investments.financial_org", 14 "query": "Trinity Ventures", 15 } 16 }] 17 }, 18 "returnStoredSource": true, 19 "returnScope": { 20 "path": "funding_rounds" 21 } 22 } 23 }, 24 { 25 "$limit": 5 26 } 27 )
[ { round_code: 'a', raised_amount: 5250000, raised_currency_code: 'USD', investments: [ { financial_org: { name: 'Frazier Technology Ventures', permalink: 'frazier-technology-ventures' }, person: null }, { financial_org: { name: 'Trinity Ventures', permalink: 'trinity-ventures' }, person: null } ] }, { round_code: 'b', raised_amount: 9500000, raised_currency_code: 'USD', investments: [ { financial_org: { name: 'Accel Partners', permalink: 'accel-partners' }, person: null }, { financial_org: { name: 'Frazier Technology Ventures', permalink: 'frazier-technology-ventures' }, person: null }, { financial_org: { name: 'Trinity Ventures', permalink: 'trinity-ventures' }, person: null } ] }, { round_code: 'c', raised_amount: 25000000, raised_currency_code: 'USD', investments: [ { financial_org: { name: 'DAG Ventures', permalink: 'dag-ventures' }, person: null }, { financial_org: { name: 'Accel Partners', permalink: 'accel-partners' }, person: null }, { financial_org: { name: 'Trinity Ventures', permalink: 'trinity-ventures' }, person: null }, { financial_org: { name: 'Frazier Technology Ventures', permalink: 'frazier-technology-ventures' }, person: null } ] }, { round_code: 'angel', raised_amount: 500000, raised_currency_code: 'USD', investments: [ { financial_org: null, person: { first_name: 'Peter', last_name: 'Thiel', permalink: 'peter-thiel' } }, { financial_org: null, person: { first_name: 'Reid', last_name: 'Hoffman', permalink: 'reid-hoffman' } } ] }, { round_code: 'a', raised_amount: 12700000, raised_currency_code: 'USD', investments: [ { financial_org: { name: 'Accel Partners', permalink: 'accel-partners' }, person: null }, { financial_org: null, person: { first_name: 'Mark', last_name: 'Pincus', permalink: 'mark-pincus' } }, { financial_org: null, person: { first_name: 'Reid', last_name: 'Hoffman', permalink: 'reid-hoffman' } } ] } ]
다음 쿼리는 range (MongoDB Search 연산자) 를 사용하여 funding_rounds.raised_amount 필드에서 5000000 이상이고 10000000 이하인 값을 쿼리하기 위해 쿼리 범위를 returnScope 옵션을 사용하여 funding_rounds 필드로 설정합니다. searchRootDocumentId 메타 필드를 그룹 키로 사용하여 각 모회사 하위의 일치하는 funding_rounds 를 그룹화합니다. 그리고 avgRaisedAmount 필드에서 회사별 평균 raised_amount 을 계산합니다. 결과를 avgRaisedAmount 로 내림차순으로 정렬합니다. 그리고 결과 수를 10 회사로 제한합니다.
1 db.companies.aggregate([ 2 { 3 "$search": { 4 "returnStoredSource": true, 5 "returnScope": { 6 "path": "funding_rounds" 7 }, 8 "range": { 9 "path": "funding_rounds.raised_amount", 10 "gte": 5000000, 11 "lte": 10000000 12 } 13 } 14 }, 15 { 16 "$group": { 17 "_id": { "$meta": "searchRootDocumentId" }, 18 "funding_rounds": { 19 "$push": { 20 "round_code": "$round_code", 21 "raised_amount": "$raised_amount", 22 "raised_currency_code": "$raised_currency_code" 23 } 24 }, 25 "avgRaisedAmount": { "$avg": "$raised_amount" } 26 } 27 }, 28 { "$sort": { "avgRaisedAmount": -1 } }, 29 { "$limit": 10 } 30 ])
[ { _id: ObjectId('52cdef7d4bab8bd675298f82'), funding_rounds: [ { round_code: 'a', raised_amount: 10000000, raised_currency_code: 'USD' } ], avgRaisedAmount: 10000000 }, { _id: ObjectId('52cdef7e4bab8bd67529af80'), funding_rounds: [ { round_code: 'b', raised_amount: 10000000, raised_currency_code: 'USD' } ], avgRaisedAmount: 10000000 }, { _id: ObjectId('52cdef7f4bab8bd67529be3d'), funding_rounds: [ { round_code: 'a', raised_amount: 10000000, raised_currency_code: 'USD' } ], avgRaisedAmount: 10000000 }, { _id: ObjectId('52cdef7f4bab8bd67529c52d'), funding_rounds: [ { round_code: 'unattributed', raised_amount: 10000000, raised_currency_code: 'USD' } ], avgRaisedAmount: 10000000 }, { _id: ObjectId('52cdef7e4bab8bd67529ab31'), funding_rounds: [ { round_code: 'c', raised_amount: 10000000, raised_currency_code: 'USD' }, { round_code: 'a', raised_amount: 10000000, raised_currency_code: 'USD' } ], avgRaisedAmount: 10000000 }, { _id: ObjectId('52cdef7e4bab8bd67529aa94'), funding_rounds: [ { round_code: 'b', raised_amount: 10000000, raised_currency_code: 'USD' } ], avgRaisedAmount: 10000000 }, { _id: ObjectId('52cdef7f4bab8bd67529be6f'), funding_rounds: [ { round_code: 'a', raised_amount: 10000000, raised_currency_code: 'USD' } ], avgRaisedAmount: 10000000 }, { _id: ObjectId('52cdef7c4bab8bd6752985cb'), funding_rounds: [ { round_code: 'd', raised_amount: 10000000, raised_currency_code: 'USD' } ], avgRaisedAmount: 10000000 }, { _id: ObjectId('52cdef7d4bab8bd675299fd1'), funding_rounds: [ { round_code: 'c', raised_amount: 10000000, raised_currency_code: 'USD' } ], avgRaisedAmount: 10000000 }, { _id: ObjectId('52cdef7f4bab8bd67529c2c8'), funding_rounds: [ { round_code: 'debt_round', raised_amount: 10000000, raised_currency_code: 'USD' } ], avgRaisedAmount: 10000000 } ]
다음 쿼리는 range (MongoDB Search 연산자) 를 사용하여 funding_rounds.raised_amount 필드에서 5000000 이상이고 10000000 이하인 값을 쿼리합니다. returnScope 옵션을 사용하여 쿼리 범위를 funding_rounds 필드로 설정합니다. 일치하는 funding_rounds 를 raised_amount 로 내림차순 정렬하고 결과를 상위 10 펀딩 라운드로 제한합니다. 그 다음 searchRootDocumentId 메타 필드를 사용하여 각 펀딩 라운드를 companies 컬렉션의 모회사에 다시 연결하고 펀딩 라운드의 round_code, raised_amount 및 raised_currency_code 필드와 함께 회사의 name 를 반환합니다.
1 db.companies.aggregate([ 2 { 3 "$search": { 4 "returnStoredSource": true, 5 "returnScope": { 6 "path": "funding_rounds" 7 }, 8 "range": { 9 "path": "funding_rounds.raised_amount", 10 "gte": 5000000, 11 "lte": 10000000 12 } 13 } 14 }, 15 { "$sort": { "raised_amount": -1 } }, 16 { "$limit": 10 }, 17 { "$addFields": { "root_id": { "$meta": "searchRootDocumentId" } } }, 18 { 19 "$lookup": { 20 "from": "companies", 21 "localField": "root_id", 22 "foreignField": "_id", 23 "as": "company" 24 } 25 }, 26 { "$unwind": "$company" }, 27 { 28 "$project": { 29 "_id": 0, 30 "round_code": 1, 31 "raised_amount": 1, 32 "raised_currency_code": 1, 33 "company.name": 1 34 } 35 } 36 ])
[ { round_code: 'partial', raised_amount: 10000000, raised_currency_code: 'USD', company: { name: 'WeFi' } }, { round_code: 'b', raised_amount: 10000000, raised_currency_code: 'USD', company: { name: 'LinkedIn' } }, { round_code: 'a', raised_amount: 10000000, raised_currency_code: 'USD', company: { name: 'Lotame' } }, { round_code: 'c', raised_amount: 10000000, raised_currency_code: 'USD', company: { name: 'OpenX' } }, { round_code: 'b', raised_amount: 10000000, raised_currency_code: 'USD', company: { name: 'AddThis' } }, { round_code: 'a', raised_amount: 10000000, raised_currency_code: 'USD', company: { name: 'Terabitz' } }, { round_code: 'b', raised_amount: 10000000, raised_currency_code: 'USD', company: { name: 'Six Apart' } }, { round_code: 'a', raised_amount: 10000000, raised_currency_code: 'USD', company: { name: 'Snocap' } }, { round_code: 'b', raised_amount: 10000000, raised_currency_code: 'USD', company: { name: 'Wikia' } }, { round_code: 'unattributed', raised_amount: 10000000, raised_currency_code: 'USD', company: { name: 'Mashery' } } ]