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

hasRoot 연산자

hasRoot

returnScope 및 returnStoredSource 옵션을 지정할 때 hasRoot 연산자를 사용하여 루트 수준 필드를 쿼리할 수 있습니다.

hasRoot 의 구문은 다음과 같습니다:

{ $search: { "index": "<index name>", // optional, defaults to "default" "hasRoot": { "operator": { <operator-specification> } }, "returnScope": { "path": "<embedded-documents-field-to-retrieve>" }, "returnStoredSource": true, }}
필드
유형
설명
필요성

operator

객체

인덱스 필드 쿼리에 사용할 연산자입니다. 자식 문서를 필터링하기 위한 검색 기준을 지정합니다.

필수 사항

이 연산자를 사용하려면 인덱스 정의에서 다음 작업을 수행해야 합니다.

  • 쿼리할 루트 수준 필드를 인덱스합니다.

  • 조회할 객체 배열을 embeddedDocuments 유형으로 인덱싱합니다. 조회하려는 중첩 필드에 대해 storedSource를 구성해야 합니다.

쿼리에서 다음을 수행해야 합니다.

  • 연산자를 검색할 필드 범위를 설정하려면 returnScope를 지정합니다.

  • storedSource 필드를 반환하려면 returnStoredSource를 true로 설정합니다.

이 섹션의 예시에서는 sample_training.companies 네임스페이스를 사용합니다. 샘플 데이터세트를 로드하고 컬렉션에 인덱스를 생성하면 이 섹션에서 설명하는 샘플 쿼리를 시도해 볼 수 있습니다.

이 섹션의 쿼리에서는 다음 인덱스를 사용합니다. 이 인덱스 정의는 MongoDB 검색을 다음 작업을 수행하도록 구성합니다.

  • 컬렉션에서 동적으로 인덱싱할 수 있는 모든 필드를 자동으로 인덱싱합니다.

  • products, funding_rounds 및 funding_rounds.investments 필드를 embeddedDocuments 유형으로 인덱스합니다.

  • mongot에 다음 필드를 저장합니다.

    • funding_rounds.raised_currency_code

    • funding_rounds.raised_amount

    • funding_rounds.investments

    • products.name

{ "mappings": { "dynamic": true, "fields": { "funding_rounds": { "dynamic": true, "fields": { "investments": [ { "dynamic": true, "type": "embeddedDocuments" } ] }, "storedSource": { "include": [ "raised_currency_code", "raised_amount", "investments" ] }, "type": "embeddedDocuments" }, "products": { "dynamic": true, "storedSource": { "include": [ "name" ] }, "type": "embeddedDocuments" } } }}

다음 쿼리는 루트 수준 필드 founded_year가 2005년과 2010년 사이에 있을 때 products을 반환합니다.

db.companies.aggregate([ { "$search": { "returnStoredSource": true, "returnScope": { "path": "products" }, "hasRoot": { "operator": { "range": { "path": "founded_year", "gte": 2005, "lte": 2010 } } } } }])

다음 쿼리는 Facebook 이라는 회사의 name 필드를 text 연산자를 사용하여 검색하고, 모든 투자자(investments.person 와 investments.financial_org)가 회사에 투자한 금액(funding_rounds.raised_amount 과 funding_rounds.raised_currency_code)을 조회합니다.

db.companies.aggregate([ { "$search": { "returnStoredSource": true, "returnScope": { "path": "funding_rounds" }, "hasRoot": { "operator": { "text": { "path": "name", "query": "Facebook" } } } } }])

다음 쿼리는 중첩된 funding_rounds.investments 필드에서 name 필드 내 어디에서든 Ventures라는 텀을 가진 금융 조직을 검색하며, 이 조직은 description 필드 내 어디에서든 network라는 텀으로 설명된 회사에 투자한 것입니다. 쿼리는 compound 연산자를 사용하여 다음을 검색합니다.

  • embeddedDocument 연산자를 사용하여 중첩된 funding_rounds.investments.financial_org.name 필드

  • 루트 수준 description 필드에 hasRoot 연산자 사용

returnScope는 연산자의 컨텍스트를 funding_rounds embeddedDocuments 유형 필드로 설정하며, returnStoredSource 필드는 true로 설정되어 storedSource 필드만 반환합니다. 이 쿼리는 결과 목록을 5개의 문서로 제한합니다.

db.companies.aggregate([ { "$search": { "compound": { "should": [ { "embeddedDocument": { "path": "funding_rounds.investments", "operator": { "wildcard": { "path": "funding_rounds.investments.financial_org.name", "query": "*Ventures*", "allowAnalyzedField": true } } } }, { "hasRoot": { "operator": { "wildcard": { "path": "description", "query": "*network*", "allowAnalyzedField": true } } } } ] }, "returnScope": {"path": "funding_rounds"}, "returnStoredSource": true } }, { "$limit": 5 }])