AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

hasRoot 演算子

hasRoot

hasRoot 演算子は、returnScope と returnStoredSource オプションを指定すると、ルートレベルのフィールドをクエリするために使用できます。

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を指定します。

  • returnStoredSource を true に設定して storedSource フィールドを返します。

このセクションの例では、sample_training.companies 名前空間を使用します。サンプル データセットをロードし、コレクションにインデックスを作成すると、このセクションで説明するサンプル クエリを試すことができます。

このセクションのクエリは以下のインデックスを使用します。このインデックス定義により、 MongoDB Search は次の操作を実行するように構成されます。

  • コレクション内のすべての動的にインデックス付け可能なフィールドを自動的にインデックス化します。

  • 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 } } } } }])

次のクエリは、nameフィールドでFacebookという名前の会社を検索し、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フィールドでVenturesという用語(nameフィールド内の任意の場所)を持つ金融組織を検索し、networkという用語(descriptionフィールド内の任意の場所)を使用して説明されている会社に投資した組織を検索します。クエリは compound 演算子を使用して以下を検索します。

  • embeddedDocument演算子を使用するネストされたfunding_rounds.investments.financial_org.nameフィールド

  • hasRoot演算子を使用するルートレベルのdescriptionフィールド

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 }])