AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
See how MongoDB 9.0 delivers up to 2x higher throughput.
MongoDB Branding Shape
Register now >
Docs Menu

hasRoot 演算子

hasRoot

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

hasRoot の構文は次のとおりです。

1{
2 $search: {
3 "index": "<index name>", // optional, defaults to "default"
4 "hasRoot": {
5 "operator": {
6 <operator-specification>
7 }
8 },
9 "returnScope": {
10 "path": "<embedded-documents-field-to-retrieve>"
11 },
12 "returnStoredSource": true,
13 }
14}
フィールド
タイプ
説明
必要性

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

1{
2 "mappings": {
3 "dynamic": true,
4 "fields": {
5 "funding_rounds": {
6 "dynamic": true,
7 "fields": {
8 "investments": [
9 {
10 "dynamic": true,
11 "type": "embeddedDocuments"
12 }
13 ]
14 },
15 "storedSource": {
16 "include": [
17 "raised_currency_code",
18 "raised_amount",
19 "investments"
20 ]
21 },
22 "type": "embeddedDocuments"
23 },
24 "products": {
25 "dynamic": true,
26 "storedSource": {
27 "include": [
28 "name"
29 ]
30 },
31 "type": "embeddedDocuments"
32 }
33 }
34 }
35}

次のクエリは、ルートレベルのフィールドfounded_yearが2005年から2010年の間の年であるproductsを返します。

1db.companies.aggregate([
2 {
3 "$search": {
4 "returnStoredSource": true,
5 "returnScope": { "path": "products" },
6 "hasRoot": {
7 "operator": {
8 "range": {
9 "path": "founded_year",
10 "gte": 2005,
11 "lte": 2010
12 }
13 }
14 }
15 }
16 }
17])

次のクエリは、nameフィールドでFacebookという名前の会社を検索し、text演算子を使用して、会社のすべての投資家(investments.personおよびinvestments.financial_org)による投資額(funding_rounds.raised_amountおよびfunding_rounds.raised_currency_code)を検索します。

1db.companies.aggregate([
2 {
3 "$search": {
4 "returnStoredSource": true,
5 "returnScope": { "path": "funding_rounds" },
6 "hasRoot": {
7 "operator": {
8 "text": {
9 "path": "name",
10 "query": "Facebook"
11 }
12 }
13 }
14 }
15 }
16])

以下のクエリは、ネストされた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件のドキュメントに制限します。

1db.companies.aggregate([
2 {
3 "$search": {
4 "compound": {
5 "should": [
6 {
7 "embeddedDocument": {
8 "path": "funding_rounds.investments",
9 "operator": {
10 "wildcard": {
11 "path": "funding_rounds.investments.financial_org.name",
12 "query": "*Ventures*",
13 "allowAnalyzedField": true
14 }
15 }
16 }
17 },
18 {
19 "hasRoot": {
20 "operator": {
21 "wildcard": {
22 "path": "description",
23 "query": "*network*",
24 "allowAnalyzedField": true
25 }
26 }
27 }
28 }
29 ]
30 },
31 "returnScope": {"path": "funding_rounds"},
32 "returnStoredSource": true
33 }
34 },
35 {
36 "$limit": 5
37 }
38])