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

$search

$search

The $search stage performs a full-text search on the specified field or fields. The field or fields must be covered by an MongoDB Search index.

$search ステージは次の環境で利用できます。

$searchパイプライン ステージには次のプロトタイプ形式があります。

{
$search: {
"index": "<index-name>",
"<operator-name>"|"<collector-name>": {
<operator-specification>|<collector-specification>
},
"highlight": {
<highlight-options>
},
"concurrent": true | false,
"count": {
<count-options>
},
"searchAfter"|"searchBefore": "<encoded-token>",
"scoreDetails": true| false,
"sort": {
<fields-to-sort>: 1 | -1
},
"returnScope": {
"path": "<embedded-documents-field-to-retrieve>"
},
"returnStoredSource": true | false,
"searchNodePreference": {
"key": <preference-string>
}
}
}

$searchステージは、次のフィールドを持つドキュメントを取得します。

フィールド
タイプ
必要性
説明

<collector-name>

オブジェクト

条件付き

クエリで使用するコレクターの名前。 このフィールドの値として、コレクター固有のオプションを含むドキュメントを提供できます。 これと<operator-name>のいずれかが必要です。

concurrent

ブール値

任意

専用検索ノードで複数のセグメントにまたがる検索を並列化します。クラスターに個別の検索ノードがない場合、 MongoDB Search はこのフラグを無視します。省略した場合、デフォルトは false になります。詳細については、「セグメント間でのクエリ実行の並列化」を参照してください。

count

オブジェクト

任意

結果のカウントを取得するためのカウント オプションを指定するドキュメント。詳しくは、「 MongoDB検索結果のカウント 」を参照してください。

highlight

オブジェクト

任意

検索タームを元のコンテキストで表示するための強調表示オプションを指定するドキュメント。

index

string

任意

使用するMongoDB Searchインデックスの名前。省略した場合、デフォルトは default になります。

If you name your index default, you don't need to specify an index parameter in the $search pipeline stage. If you give a custom name to your index, you must specify this name in the index parameter.

MongoDB Search では、インデックス名のスペルをうっかり間違えた場合、または指定されたインデックスがクラスターにまだ存在しない場合は結果が返されません。

<operator-name>

オブジェクト

条件付き

検索する演算子の名前。 このフィールドの値として、演算子固有のオプションを含むドキュメントを提供できます。 これと<collector-name>のいずれかが必要です。 複数の演算子を含む複合クエリを実行するには、複合演算子 を使用します。

returnScope

オブジェクト

任意

指定された埋め込みドキュメントフィールドにクエリのコンテキストを設定するオブジェクト。returnStoredSource を指定し、それを true に設定する必要があります。

returnStoredSource

ブール値

条件付き

バックエンドデータベースでドキュメント全体を検索するか、保存されたソースフィールドのみを MongoDB Search から直接返すかを指定するフラグです。省略された場合、デフォルトは false となります。returnScopeを指定する場合、trueでなければなりません。

詳細を学ぶには、「保存されたソースフィールドを返す」を参照してください。

searchAfter

string

任意

Reference point for retrieving results. searchAfter returns documents starting immediately following the specified reference point. The reference point must be a Base64-encoded token generated by the $meta keyword searchSequenceToken. To learn more, see Paginate the Results. This field is mutually exclusive with the searchBefore field.

searchBefore

string

任意

Reference point for retrieving results. searchBefore returns documents starting immediately before the specified reference point. The reference point must be a Base64-encoded token generated by the $meta keyword searchSequenceToken. To learn more, see Paginate the Results. This field is mutually exclusive with the searchAfter field.

searchNodePreference

オブジェクト

任意

このクエリの優先ルーティングを有効にするドキュメント。このフィールドに値を設定すると、Atlas は、その値が同じである限り、毎回このクエリを同じ検索ノードに対して実行し、デフォルトのクエリルーティング動作を上書きします。

このドキュメントには、key の値が任意の string であるキーと値のペアが含まれています。

重要: この設定を有効にすると検索結果の整合性が向上しますが、永続的な整合性は保証されません。データセット、インデックス構造、 クラスタートポロジー、または優先ノードの可用性を変更すると、依然として不整合が発生する可能性があります。

scoreDetails

ブール値

任意

Flag that specifies whether to retrieve a detailed breakdown of the score for the documents in the results. If omitted, defaults to false. To view the details, you must use the $meta expression in the $project stage. To learn more, see Return the Score Details.

sort

オブジェクト

任意

MongoDB Search の結果を で昇順または降順でソートするフィールドを指定するドキュメント。日付、数値(整数、浮動小数、double 値)、string 値でソートできます。詳しくは、 MongoDB検索結果の並べ替え を参照してください。

$search must be the first stage of any pipeline it appears in. $search cannot be used in:

$search returns only the results of your query. The metadata results of your $search query are saved in the $$SEARCH_META aggregation variable. You can use the $$SEARCH_META variable to view the metadata results for your $search query.

The $$SEARCH_META aggregation variable can be used anywhere after a $search stage in any pipeline, but it can't be used after the $lookup or $unionWith stage in any pipeline. The $$SEARCH_META aggregation variable can't be used in any subsequent stage after a $searchMeta stage.

sample_mflix.moviesコレクションに次のインデックスがあるとします。

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

The following query searches for movies released near September 01, 2011 using the $search stage. The query includes a:

  • $project stage to exclude all fields in the documents except title and released.

  • $facet ステージは、 を出力します。

    • docs 上位の5検索結果の配列を含むフィールド

    • meta フィールド( $$SEARCH_META変数の値を持つもの)

db.movies.aggregate([
{
"$search": {
"near": {
"path": "released",
"origin": ISODate("2011-09-01T00:00:00.000+00:00"),
"pivot": 7776000000
}
}
},
{
$project: {
"_id": 0,
"title": 1,
"released": 1
}
},
{ "$limit": 5 },
{
"$facet": {
"docs": [],
"meta": [
{"$replaceWith": "$$SEARCH_META"},
{"$limit": 1}
]
}
}
])
{
"docs" : [
{
"title" : "Submarino",
"released" : ISODate("2011-09-01T00:00:00Z")
},
{
"title" : "Devil's Playground",
"released" : ISODate("2011-09-01T00:00:00Z")
},
{
"title" : "Bag It",
"released" : ISODate("2011-09-01T00:00:00Z")
},
{
"title" : "Dos",
"released" : ISODate("2011-09-01T00:00:00Z")
},
{
"title" : "We Were Here",
"released" : ISODate("2011-09-01T00:00:00Z")
}
],
"meta" : [
{ "count" : { "lowerBound" : NumberLong(17373) } }
]
}

$$SEARCH_META変数とその使用方法の詳細については、

If you are experiencing issues with your MongoDB Search $search queries, see Troubleshoot Queries.