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

facet (MongoDB Search 演算子)

facet

facet コレクターは、指定されたファセット フィールドの値または範囲で結果をグループ化し、各グループのカウントを返します。

You can use facet with both the $search and $searchMeta stages. MongoDB recommends using facet with the $searchMeta stage to retrieve metadata results only for the query. To retrieve metadata results and query results using the $search stage, you must use the $$SEARCH_META aggregation variable. See SEARCH_META Aggregation Variable to learn more.

embeddedDocuments フィールド型定義で storedSource を定義すると、 returnStoredSource とともに returnScope を使用して、オブジェクト配列内のネストされたフィールドにファセットを設定できます。それ以外の場合は、ルートの embeddedDocuments 型フィールドに対してのみファセットを設定できます。ファセットの例:

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

{
"$searchMeta"|"$search": {
"index": <index name>, // optional, defaults to "default"
"facet": {
"operator": {
<operator-specifications>
},
"facets": {
<facet-definitions>
}
},
"returnScope": {
"path": "<embedded-documents-field-to-query>"
},
"returnStoredSource": true
}
}
フィールド
タイプ
必須
説明

facets

ドキュメント

はい

Information for bucketing the data for each facet. You must specify at least one Facet Definition. Each facet definition can also specify Facet Aggregations to compute metrics for each bucket.

operator

ドキュメント

no

ファセットを実行するために使用する演算子 。省略した場合、 MongoDB Search はコレクション内のすべてのドキュメントに対してファセットを実行します。

Facet queries are memory-intensive, regardless of your cluster tier. Queries that specify Facet Aggregations or nested facets require more memory and disk than other facet queries. Before you use facets in production, confirm that your cluster has enough memory for your workload. To learn more, see Search Memory Management. To get sizing guidance for your workload, request support.

ファセットクエリが必要とするメモリは、numBuckets または boundaries を使用してリクエストするバケットの数ではなく、ファセットするフィールドの一意の値の数によって異なります。例、500 万個の一意の値を持つフィールドで 20 個のバケットをリクエストするファセットでは、上位 20 個のバケットを決定するためにすべての一意の値をカウントします。

For nested facets, the total unique values computed in a query multiply based on the unique values across each level of nesting. Even when each individual field has low cardinality, the combination of values across nested paths can produce a large number of unique buckets. To learn more about how nested facets multiply buckets, see Nested Facets.

MongoDB Searchはクエリごとにバケット数を個別に追跡するため、合計メモリ使用量は同時実行されるファセットクエリの数に応じて増加します。シャーディングされたクラスターでは、MongoDB Searchは各シャードからのバケットをマージするためにもメモリを必要とします。このメモリ使用量は、シャードの数とバケットの数に応じて増加します。

ファセット定義ドキュメントには、ファセット名とファセットのタイプに固有のオプションが含まれています。MongoDB Search は次の型のファセットをサポートしています。

重要

stringFacet は廃止予定です。代わりにトークンを使用することで、ファセットが改善されます。

ファセットの更新されたフィールド型と古いフィールド型の違いの詳細については、ファセットのフィールド型の比較を参照してください。

文字列ファセットを使用すると、指定された文字列フィールド内の最も頻繁に出現する文字列値に基づいて、MongoDB Search 結果を絞り込むことができます。文字列フィールドは、トークンとしてインデックス付けする必要があることに注意してください。埋め込みドキュメント内のstringフィールドをファセットするには、親フィールドもドキュメント型としてインデックス付けする必要があります。配列内の文字列または埋め込みドキュメントをファセットすると、MongoDB Search は一致するルート ドキュメントの数に基づいてファセット数を返します。

文字列ファセットの構文は次のとおりです。

{
"$searchMeta": {
"facet":{
"operator": {
<operator-specification>
},
"facets": {
"<facet-name>" : {
"type" : "string",
"path" : "<field-path>",
"numBuckets" : <number-of-categories>,
"embeddedDocument": { <embedded-document-scope> } // optional
}
}
}
}
}
オプション
タイプ
説明
必須

embeddedDocument

オブジェクト

Scopes the facet's bucket counts to only the child documents of an embeddedDocuments array that match a query predicate. To learn more, see Facet on Matching Children of an Embedded Documents Array.

no

numBuckets

整数

結果で返されるファセット カテゴリの最大数。値は 10000 以下である必要があります。指定した場合、データがリクエストされた数よりも少ないカテゴリにグループ化されると、MongoDB Search はリクエストされた数よりも少ないカテゴリを返すことがあります。省略した場合、デフォルトは 10 になります。つまり、MongoDB Search はカウントで上位 10 個のファセット カテゴリのみを返します。

no

path

string

ファセットするフィールドパス。トークンとしてインデックス付けされたフィールドを指定できます。

はい

type

string

ファセットの型。値はstringでなければなりません。

はい

例

次の例では、sample_mflix.movies コレクションで default という名前のインデックスを使用します。コレクション内の genres フィールドはトークン型としてインデックス付けされ、year フィールドは数値型としてインデックス付けされます。

{
"mappings": {
"dynamic": false,
"fields": {
"genres": {
"type": "token"
},
"year": {
"type": "number"
}
}
}
}

クエリは、$searchMeta ステージを使用して、movies コレクションの year フィールドで 2000 年から 2015 年までの映画を検索し、各ジャンルの映画の数を検索します。

1db.movies.aggregate([
2 {
3 "$searchMeta": {
4 "facet": {
5 "operator": {
6 "range": {
7 "path": "year",
8 "gte": 2000,
9 "lte": 2015
10 }
11 },
12 "facets": {
13 "genresFacet": {
14 "type": "string",
15 "path": "genres"
16 }
17 }
18 }
19 }
20 }
21])

これらの結果の詳細については、「ファセット結果」を参照してください。

重要

numberFacet は廃止予定です。代わりに数値を使用してください。これにより、ファセットが改善されます。

ファセットの更新されたフィールド型と古いフィールド型の違いの詳細については、ファセットのフィールド型の比較を参照してください。

数値ファセットを使用すると、結果を個別の数値の範囲に分割して、検索結果内の数値の頻度を判断できます。配列または埋め込みドキュメント内の数値をファセットと、 MongoDB Search は一致するルート ドキュメントの数に基づいてファセット数を返します。

数値ファセットの構文は次のとおりです。

{
"$searchMeta": {
"facet":{
"operator": {
<operator-specification>
},
"facets": {
"<facet-name>" : {
"type" : "number",
"path" : "<field-path>",
"boundaries" : <array-of-numbers>,
"default": "<bucket-name>",
"embeddedDocument": { <embedded-document-scope> } // optional
}
}
}
}
}
オプション
タイプ
説明
必須

boundaries

数字の配列

List of numeric values in ascending order that specify the boundaries for your buckets. You must specify between two and ten thousand ([2, 10000]) boundary values. Each adjacent pair of values defines a bucket with an inclusive lower bound and an exclusive upper bound. You can specify any combination of values of the following BSON types:

  • 32 ビット整数(int32)

  • 64 ビット整数(int64)

  • 64 ビットバイナリ浮動小数点(double)

はい

default

string

指定された境界内に含まれない、演算子から返されたドキュメントをカウントする追加のバケットの名前。省略した場合、 MongoDB Search には、指定されたバケットに該当しないファセット演算子の結果も含まれますが、バケット数には含まれません。

no

embeddedDocument

オブジェクト

Scopes the facet's bucket counts to only the child documents of an embeddedDocuments array that match a query predicate. To learn more, see Facet on Matching Children of an Embedded Documents Array.

no

path

string

ファセットするフィールドパス。数値型としてインデックス付けされたフィールドを指定できます。

はい

type

string

ファセットの型。値はnumberでなければなりません。

はい

例

次の例では、sample_mflix.movies コレクションで default という名前のインデックスを使用します。コレクション内の year フィールドは、数値型としてインデックスされます。

{
"mappings": {
"dynamic": false,
"fields": {
"year": [
{
"type": "number"
}
]
}
}
}

クエリは、$searchMeta ステージを使用して、movies コレクションの year フィールドで 1980 年から 2000 年までの映画を検索し、クエリのメタデータ結果を検索します。クエリでは 3 つのバケットを指定します。

  • 1980(このバケットの下限値を含む)

  • 1990(1980 バケットの上限(排他的)とこのバケットの下限(包括的))

  • 2000(1990 バケットの排他的上限)

クエリでは、指定された境界のいずれにも該当しないクエリの結果を検索するために、other という名前の default バケットも指定します。

1db.movies.aggregate([
2 {
3 "$searchMeta": {
4 "facet": {
5 "operator": {
6 "range": {
7 "path": "year",
8 "gte": 1980,
9 "lte": 2000
10 }
11 },
12 "facets": {
13 "yearFacet": {
14 "type": "number",
15 "path": "year",
16 "boundaries": [1980,1990,2000],
17 "default": "other"
18 }
19 }
20 }
21 }
22 }
23])

これらの結果の詳細については、「ファセット結果」を参照してください。

重要

dateFacet は廃止予定です。代わりに date を使用することで、ファセットが改善されます。

ファセットの更新されたフィールド型と古いフィールド型の違いの詳細については、ファセットのフィールド型の比較を参照してください。

日付ファセットを使用すると、日付に基づいて検索結果を絞り込むことができます。配列または埋め込みドキュメント内の日付をファセットと、 MongoDB Search は一致するルート ドキュメントの数に基づいてファセット数を返します。

日付ファセットの構文は次のとおりです。

{
"$searchMeta": {
"facet":{
"operator": {
<operator-specification>
},
"facets": {
"<facet-name>" : {
"type" : "date",
"path" : "<field-path>",
"boundaries" : <array-of-dates>,
"default": "<bucket-name>",
"embeddedDocument": { <embedded-document-scope> } // optional
}
}
}
}
}
オプション
タイプ
説明
必須

boundaries

数字の配列

各バケットの境界を指定する日付値のリスト。以下を指定する必要があります:

  • 少なくとも 2 つの境界を設定し、それらは 10,000([2, 10000])以下とします。

  • 値は昇順で表示され、日付が最も古いものから順に表示されています

隣接する各値のペアは、バケットの包括的な下限と排他的な上限として機能します。

はい

default

string

指定された境界内に含まれない、演算子から返されたドキュメントをカウントする追加のバケットの名前。省略した場合、 MongoDB Search には指定されたバケットに該当しないファセット演算子の結果も含まれますが、 MongoDB Search はこれらの結果をバケットカウントに含めません。

no

embeddedDocument

オブジェクト

Scopes the facet's bucket counts to only the child documents of an embeddedDocuments array that match a query predicate. To learn more, see Facet on Matching Children of an Embedded Documents Array.

no

path

string

ファセットするフィールドパス。日付型としてインデックス付けされたフィールドを指定できます。

はい

type

string

ファセットの型。値はdateでなければなりません。

はい

例

次の例では、sample_mflix.movies コレクションで default という名前のインデックスを使用します。コレクション内の released フィールドは、date 型としてインデックス付けされます。

{
"mappings": {
"dynamic": false,
"fields": {
"released": [
{
"type": "date"
}
]
}
}
}

クエリは、$searchMeta ステージを使用して、movies コレクションの released フィールドで 2000 年から 2015 年までの映画を検索し、クエリのメタデータ結果を検索します。このクエリでは、次の 4 つのバケットを指定します。

  • 2000-01-01(このバケットの下限値を含む)

  • 2005-01-01(2000-01-01 バケットの上限(排他的)とこのバケットの下限(包括的))

  • 2010-01-01(2005-01-01 バケットの上限(排他的)とこのバケットの下限(包括的))

  • 2015-01-01(2010-01-01 バケットの排他的上限)

クエリでは、指定された境界のいずれにも該当しないクエリの結果を検索するために、other という名前の default バケットも指定します。

1db.movies.aggregate([
2 {
3 "$searchMeta": {
4 "facet": {
5 "operator": {
6 "range": {
7 "path": "released",
8 "gte": ISODate("2000-01-01T00:00:00.000Z"),
9 "lte": ISODate("2015-01-31T00:00:00.000Z")
10 }
11 },
12 "facets": {
13 "yearFacet": {
14 "type": "date",
15 "path": "released",
16 "boundaries": [ISODate("2000-01-01"), ISODate("2005-01-01"), ISODate("2010-01-01"), ISODate("2015-01-01")],
17 "default": "other"
18 }
19 }
20 }
21 }
22 }
23])

これらの結果の詳細については、「ファセット結果」を参照してください。

更新されたMongoDB検索フィールドタイプは、古いタイプ(stringFacet、numberFacet、dateFacet)と比較して、ファセットをサポートする機能が改善されています。次の表では、機能における主な違いをまとめています。

ファセットカテゴリ
更新されたフィールドタイプ
古いファセットの型
重要な違い

文字列

stringFacet(廃止予定)

Normalizer Support: The token type supports normalizers that transform facet buckets. For example, with normalizer: lowercase, "ADIDAS" and "adidas" count toward the same bucket while stringFacet treats them as separate buckets.

Numeric

numberFacet(廃止予定)

Array Support: The number type considers values within arrays for facet buckets. For example, a document with an array value [0, 10] counts toward both buckets [1, 5] and [6, 10] while numberFacet ignores array values completely.

日付

dateFacet(廃止予定)

配列サポート: date タイプは、ファセットバケットの配列内の値を考慮します。例、日付を含む配列値は複数の日付範囲バケットに貢献できますが、dateFacet は配列値を完全に無視します。

注意

古いフィールド型と更新されたフィールド型の両方が同じフィールドで定義されている場合は、古いファセット型が優先されます。例、フィールドに token と stringFacet の両方が定義されている場合、ファセット計算では stringFacet マッピングが使用されます。

各ファセット定義には、任意の embeddedDocument フィールドを含めることができます。このフィールドは、ファセットのバケットカウントを、クエリ述語に一致する embeddedDocuments 配列内の子ドキュメントのみにスコープします。embeddedDocument フィールドを省略した場合、ファセットはドキュメントインデックスになります。

embeddedDocumentがない場合、ルートドキュメントは、facet.operatorブロックで定義されたクエリに一致しない子ドキュメントであっても、そのバケットに該当する子ドキュメントがあれば、そのバケットでカウントされます。embeddedDocumentフィールドは、バケットへの寄与を、指定されたembeddedDocument.operatorに一致する子のみに制限します。各ルートドキュメントは、バケットごとに最大1回カウントされます。

embeddedDocument フィールドは、次のフィールドがあります。

フィールド
タイプ
説明
必須

path

string

ファセットのスコープを絞り込むためのインデックス付き embeddedDocuments 型フィールド。ファセットの path は、このフィールドの子孫である必要があります。

はい

operator

オブジェクト

path で指定する embeddedDocuments 配列内の各子ドキュメントに対して実行する 演算子。この演算子に一致する子ドキュメントのみが、ファセットのバケットカウントに寄与します。この演算子で参照されるすべてのパスは、path の子孫である必要があります。

はい

For a number or date facet, a matching child whose value falls outside the configured boundaries counts in the default bucket, if you specify one, using the same rule as a document-indexed facet. A root document with no matching child doesn't contribute to the embeddedDocument-scoped facet, but can still contribute to any other, unscoped facet in the same query.

重要

You can't combine an embeddedDocument-scoped facet with several other faceting options. To learn more, see Limitations.

For an example, see the Embedded-Scoped Facet Example.

重要

Facet aggregations is in Preview. The feature and corresponding documentation might change at any time during the Preview period. To learn more, see Preview Features.

To compute metrics for each facet bucket, add an aggregations document to a facet definition. MongoDB Search computes the metrics from the MongoDB Search index, so it is much more efficient than running a $group stage later in your pipeline.

各ファセット バケットでは、aggregations を使用して次の値を計算できます。

  • 合計

  • 平均

  • minimum

  • maximum

  • 最初に

  • last

aggregationsを使用する場合は、次のことを行う必要があります。

  • 各メトリクスのpathで指定するフィールドをtoken、number、またはdateとしてインデックス化します。

  • バケット数が必要な場合は、count メトリクスを明示的に含めてください。

  • aggregationsは、ネストされたファセットのリーフレベルでのみ指定してください。

    中間的なグループ化のメトリクスを計算する場合は、そのグループ化を aggregations を使用した個別のトップレベルのファセットとして指定し、ネストされたファセットは指定しないでください。

MongoDB Search は、MongoDB Search インデックス内のデータに対して aggregations を計算します。このインデックスは 終極的にコンシステントです。

注意

メモリ要件

Facet queries that specify aggregations or nested facets increase the memory and disk that your MongoDB Search index requires beyond the requirements for other facet queries. To learn more, see Memory Requirements.

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

{
"$searchMeta": {
"facet": {
"facets": {
"<facet-name>": {
"type": "<facet-type>",
"path": "<field-path>",
"aggregations": {
"<aggregation-name>": {
"type": "sum" | "min" | "max" | "avg" | "first" | "last",
"path": "<field-path>"
}
}
}
}
}
}
}

aggregations ドキュメントの各キーは、指定した集計名です。MongoDB Search は、指定した名前の下にあるバケットドキュメント内の各集計を返します。

オプション
タイプ
説明
必須

path

string

メトリクスを計算するフィールドパス。これは、pathを受け入れないcountを除くすべてのメトリクスタイプで必須です。他のすべてのメトリクスタイプは2番目のフィールドから値を計算するため、そのフィールドに名前を付ける必要があります。例として、バケット内のドキュメントの平均評価を返すには、avgをratingのpathで指定します。countメトリクスはバケット内のドキュメントをカウントするため、2番目のフィールドは必要ありません。

条件付き

type

string

計算するメトリクスのタイプ。値は次のいずれかになります。

  • count

  • sum

  • avg

  • min

  • max

  • first

  • last

To learn more about each metric type, see Metric Types.

はい

タイプ
フィールドの要件
説明

count

なし

バケット内のドキュメントをカウントします。このメトリクスタイプには path を指定しないでください。ファセット定義から aggregations を省略した場合、MongoDB Search はバケットに対して count メトリクスを返します。aggregations を指定した場合、MongoDB Search は aggregations ドキュメントで count を指定した場合にのみ count メトリクスを返します。

sum

バケット内のドキュメントについて、指定されたフィールドの値を加算します。

avg

バケット内のドキュメントの指定したフィールドの値の平均を返します。bucket 内のドキュメントのいずれにもフィールドの値が設定されていない場合、MongoDB Search は null を返します。

min

なし

バケット内のドキュメントについて、指定されたフィールドの最小値を返します。

max

なし

バケット内のドキュメントについて、指定されたフィールドの最大値を返します。

first

なし

クエリのソートオプションで定義された順序に従って、バケット内の最初のドキュメントの指定されたフィールドの値を返します。クエリでソートが指定されていない場合、順序は任意です。ソートを指定する方法を学ぶには、MongoDB Search結果のソートを参照してください。

last

なし

クエリのソートオプションで定義された順序に従って、バケット内の最後のドキュメントの指定されたフィールドの値を返します。クエリでソートが指定されていない場合、順序は任意です。ソートを指定する方法を学ぶには、MongoDB Search結果のソートを参照してください。

重要

min また、maxはtokenを含むすべてのフィールドタイプをサポートしています。MongoDB Searchがこれらのメトリクスを計算する際、フィールドがサポートされていない型に解決されるドキュメントはスキップされます。MongoDB Searchは、ドキュメントをスキップしてもエラーを返しません。MongoDB Searchがバケット内のドキュメントをスキップしたかどうかを確認するには、同じaggregationsドキュメントにcountメトリクスを追加し、期待されるドキュメント数と比較します。

For values that MongoDB Search doesn't skip, min selects the smallest value in the bucket and max selects the largest. If the values aren't all of the same type, MongoDB Search applies BSON type comparison order to determine which value is smallest or largest.

各メトリクスタイプは、配列のように複数の値に解決されるフィールドをそれぞれ異なる方法で取り扱います。

  • sum および avg ドキュメントをスキップします。

  • min そして max 配列内の最小値と最大値を選択し、ドキュメントの代表要素として使用します。

  • first および last は、すべての値を配列として返します。

first および last の場合、返される配列は、次の点でソースドキュメントと異なります。

  • 値の順序は異なる場合があります。MongoDB Searchはインデックスから値を読み取りますが、これはソースドキュメントの順序を保持しません。例として、ドキュメントに["red", "blue", "green"]が含まれている場合、MongoDB Searchは["blue", "green", "red"]を返す可能性があります。

  • MongoDB Searchは、重複する値のインスタンスを1つだけ返します。

  • MongoDB Searchは、ファセットに使用できない型の値を含め、パスにあるすべての値を返します。例として、配列に文字列とブール値の両方が含まれている場合、MongoDB Searchは文字列とともにブール値を返します。

ソースドキュメントに含まれているとおりに値を返すには、インデックス定義でフィールドの storedSource を定義し、クエリで returnStoredSource を true に設定します。

重要

Nested facets is in Preview. The feature and corresponding documentation might change at any time during the Preview period. To learn more, see Preview Features.

To group the documents in each bucket by additional fields, add a facets document to an existing facet definition. Each nested facet definition uses the same syntax as a top-level facet definition, so you can nest facets to build a tree of groupings. MongoDB Search returns the buckets for a nested facet inside each bucket of its parent facet.

For each bucket of a facet that contains a nested facet, MongoDB Search also returns a count field with the number of documents in the bucket. You don't need to specify a count metric for these intermediate buckets.

You can nest facets without limit on the number of levels. However, the following restrictions apply:

  • aggregations は、ネストされたファセットツリーのリーフファセットでのみ指定できます。facets を指定するファセット定義では、aggregations を指定することもできません。中間グループ化のメトリクスを計算するには、そのグループ化を aggregations を持つ個別のトップレベルファセットとして指定し、ネストされたファセットは指定しません。

  • You can't name a nested facet count. MongoDB Search reserves count for the document count of intermediate buckets.

  • クエリが返すことができる一意のバケットの総数は、100000を超えることはできません。1つのファセットツリーが生成するバケットを計算するには、そのネストされたブランチの各レベルにあるバケットの数を掛け合わせます。レベルごとのバケット数は、string ファセットの場合は numBuckets、数値または日付ファセットの場合は boundaries と default で定義されるバケット数になります。例として、20個のバケットをリクエストし、20個のバケットをリクエストするネストされたファセットを含むファセットは、400個の一意のバケットを生成します。各レベルでこの合計が乗算されるため、各レベルでより多くのバケットをリクエストすると、ネストできるレベルの数が減ります。

ファセットクエリの場合、 MongoDB Search は、定義されたファセット名と、そのファセットのバケットの配列とのマッピングを結果に返します。ファセット結果ドキュメントには、ファセットの結果バケットの配列である buckets オプションが含まれています。配列内の各ファセットバケットドキュメントには、次のフィールドがあります。

オプション
タイプ
説明

_id

オブジェクト

このファセット バケットを識別するユニークな識別子。この値は、ファセットされるデータ型と一致します。

count

整数

このファセットバケット内のドキュメントの数。countフィールドの詳細については、 MongoDB検索結果のカウント を参照してください。

If the facet definition specifies Facet Aggregations, each bucket document contains the metrics that you name in the aggregations document instead of the count field. If the facet definition specifies nested facets, each bucket document contains a count field with the number of documents in the bucket, and the results for the nested facet.

MongoDB Search を使用すると、同じファセット内の複数のバケットを同時に表示および選択できます。通常、ファセット内でバケットを選択すると、その選択に従って検索結果がフィルタリングされ、すべてのファセットのカウントが変更されます。

例

sample_airbnb.listingsコレクションのインデックス定義で、次のフィールドのファセットが指定されているとします。

  • cancellation_policy

  • room_type

  • accommodates

cancellation_policyファセットには次のバケットがあります。

  • flexible

  • moderate

  • strict_14_with_grace_period

  • super_strict_30

  • super_strict_60

各バケットには、独自の結果カウントがあります。moderate cancellation_policy を検索すると、他の 4 つのバケットのカウントは 0 になります。さらに、room_type ファセットと accommodates ファセットのバケットのカウントは、同様に flexiblecancellation_policy を持つ各バケットの結果の数に減ります。

ファセットが検索結果のカウントに与える影響をより細かく制御する必要があるシナリオでは、ファセット クエリで doesNotAffectプロパティを使用して複数選択のファセットを有効にします。これらのファセットは引き続き結果をフィルタリングしますが、クエリは結果数を変更しません。

以下の演算子で doesNotAffect を指定できます。

複数のファセットを除外するには、ファセット名の配列を指定します。ほとんどのユースケースでは、フィルターとパスを共有するファセットのみを除外します。例として、rating フィールドに対する equals フィルターは、rating をパスとして使用するファセットの名前を指定します。

注意

The doesNotAffect option doesn't work with facets that specify aggregations. To learn more, see Limitations.

例

moderate cancellation_policy があるドキュメントのsample_airbnb.listingsAndReviews コレクションに対するクエリを検討します。doesNotAffect の値を cancellation_policy に指定すると、cancellation_policy ファセットのバケットのカウントは変更されませんが、他のファセットのバケットの結果カウントは、同様に moderatecancellation_policy を持つ各バケットの結果の数に減ります。

詳細については、複数選択ファセットの例を参照してください。

You can't combine doesNotAffect with an embeddedDocument-scoped facet. To learn more, see Facet on Matching Children of an Embedded Documents Array.

最後に、多くのファセットを持つユースケースでは、特定のファセットに影響する他のフィルターを制限できます。これを行うには、他のフィールドのファセットを含め、任意のフィルターの doesNotAffect プロパティで任意のファセットを指定します。これにより、どの選択がより迅速に、またはより緩やかに選択肢を絞り込むかを一目で確認できます。

例

accommodates 値が 3 であるドキュメントの sample_airbnb.listingsAndReviews コレクションに対するクエリを検討します。doesNotAffect の値として cancellation_policy を指定した場合、room_type バケットの結果数は、3 人も収容できる各バケット内の結果数に減少しますが、cancellation_policy のバケットの結果数は影響を受けません。

詳細については、「ファセット間フィルター除外の例」を参照してください。

When you run your query using the $search stage, MongoDB Search stores the metadata results in the $$SEARCH_META variable and returns only the search results. You can use the $$SEARCH_META variable in all the supported aggregation pipeline stages to view the metadata results for your $search query. You can also use the $$SEARCH_META variable with facet.aggregations.

MongoDB では、検索結果とメタデータ結果の両方が必要な場合にのみ $$SEARCH_META 変数を使用することを推奨します。それ以外の場合は、次を使用します。

  • $search ステージを使用して、検索結果のみを取得します。

  • $searchMeta ステージを使用して、メタデータ結果のみを取得します。

ファセット クエリは単一のフィールドに対してのみ実行できます。フィールドのグループに対してファセット クエリを実行することはできません。

The following limitations apply to Facet on Matching Children of an Embedded Documents Array:

  • 同じクエリ内で、トップレベルの returnScope オプションと embeddedDocument スコープのファセットを結合することはできません。

  • embeddedDocumentスコープのファセットはカウントのみを返します。

  • ドリルサイドウェイズファセット(doesNotAffect)とembeddedDocumentスコープのファセットを同じクエリで組み合わせることはできません。

The following limitations apply to Facet Aggregations:

  • aggregations では、次のオプションは使用できません。

  • You can't facet or compute aggregations on the following BSON types:

    • binData

    • bool

    • decimal

    • null

    • objectId

    • timestamp

    To compute metrics over one of these types, use a view to convert the field to a supported type before you index it.

The following examples use the sample data. The metadata results example demonstrates how to run a $searchMeta query with facet to retrieve only the metadata in the results. The metadata and search results example demonstrates how to run a $search query with facet and the $SEARCH_META aggregation variable to retrieve both the search and metadata results. The returnScope example demonstrates how to facet on nested fields in an array of objects dynamically indexed using the embeddedDocuments type. The embedded-scoped facet example demonstrates how to facet on only the matching children of an embeddedDocuments array. The multi-select and inter-facet filter exclusion examples demonstrate how to use doesNotAffect to control how facets affect each other's counts. The aggregation example demonstrates how to compute metrics for each facet bucket, and the nested facet example demonstrates how to group results by a second field and compute metrics for each bucket.

詳しくは、 MongoDB Searchでファセットの使用方法 を参照してください。

You can learn more about facet (MongoDB Search Operator) in MongoDB Search with our course and video.

To learn more about using facets in MongoDB Search, take Unit 9 of the Intro To MongoDB Course on MongoDB University. The 1.5 hour unit includes an overview of MongoDB Search and lessons on creating MongoDB Search indexes, running $search queries using compound operators, and grouping results using facet.

Watch this video to learn about how you can create and use a numeric and string facet (MongoDB Search Operator) in your query to group results and retrieve a count of the results in the groups.

所要時間: 11 分

このページを評価