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

クエリ参照

MongoDB Search queries take the form of an aggregation pipeline stage. MongoDB Search provides $search and $searchMeta stages, both of which must be the first stage in any query pipeline, including the $lookup and $unionWith sub-pipelines. These stages can be used in conjunction with other aggregation pipeline stages in your query pipeline.

選択したパイプライン ステージに基づいて、クエリはフルテキスト検索の検索結果、または検索結果に関するメタデータのいずれかを返します。

集計パイプライン ステージ
目的

全文検索の検索結果を返します。

検索結果に関するメタデータを返します。

MongoDB Search はクエリ 演算子コレクション も備えています。これらツールは $search$searchMeta 集計パイプラインステージ内で使用できます。MongoDB Search 演算子を使用すると、クラスターのコレクションから関連データを検索して取得できます。コレクターは、メタデータの検索結果を表すドキュメントを返します。

MongoDB Search 演算子を使用して、ターム、フレーズ、地理的形状とポイント、数値、類似ドキュメント、シノニム(同意語)などをクエリできます。

You can also search using regex and wildcard expressions. The MongoDB Search compound operator allows you to combine multiple operators inside your $search stage to perform a complex search and filter of data based on what must, must not, or should be present in the documents returned by MongoDB Search. You can use the compound operator to also match or filter documents in the $search stage itself. Running $match after $search is less performant than running $search with the compound operator.

演算子とコレクターの詳細については、「演算子とコレクター」を参照してください。

mongod 同じノードで とmongot

When you run a query, MongoDB Search uses the configured read preference to identify the node on which to run the query. The query first goes to the MongoDB process, which is mongod for a replica set cluster or mongos for a sharded cluster.

For a replica set cluster, the MongoDB process routes the query to the mongot on the same node. For sharded clusters, your cluster data is partitioned across mongod instances and each mongot knows about the data on the mongod on the same node only. Therefore, you can't run MongoDB Search queries that target a particular shard. mongos directs the queries to all shards, making these scatter gather queries. If you use zones to distribute a sharded collection over a subset of the shards in the cluster, MongoDB Search routes the query to the zone that contains the shards for the collection that you are querying and runs your $search queries on just the shards where the collection is located.

MongoDB Search は検索とスコアリングを実行し、一致する結果のドキュメントID とその他の検索メタデータを mongod に返します。次に、mongod は一致する結果のクエリをドキュメント全体で暗黙的に実行し、その結果をクライアントに返します。

クエリで $search 同時実行オプションを使用すると、MongoDB Search はクエリ内並列処理を有効にします。詳細については、「セグメント間でのクエリ実行の並列化」を参照してください。

mongod 異なるノードの とmongot

When you run a query, the query first goes to the mongod based on the configured read preference. By default, the mongod process routes the search query through a load balancer on the same node, which distributes the requests across all of the mongot processes. Alternatively, if you set the searchNodePreference parameter for your $search stage, MongoDB Search runs your query against the same search node each time.

MongoDB Search mongot プロセスは検索とスコアリングを実行し、一致する結果のドキュメントID とメタデータを mongod に返します。次に、mongod はドキュメント全体で一致する結果の検索を実行し、その結果をクライアントに返します。クエリで $search 同時実行 オプションを使用すると、 MongoDB Search はクエリ内並列処理を有効にします。詳細については、セグメント間でのクエリ実行の並列化 を参照してください。

MongoDB Search は関連性ベースのスコアを結果セット内の各ドキュメントに関連付けます。関連性を基準にスコアリングすることで、 MongoDB Search は最高スコアから最低スコアの順にドキュメントを返すことができます。MongoDB Search は、クエリタームがドキュメントに頻繁に表示される場合は高く、クエリタームがコレクション内の多数のドキュメントにわたり表示される場合は低くなります。MongoDB Search では、boost、decay などのスコアの変更関数を使用することで、関連性ベースのデフォルトスコアをカスタマイズすることもできます。結果内のスコアをカスタマイズする方法の詳細については、「結果内のドキュメントのスコアリング」を参照してください。

Tip

See: Learn by Watching

MongoDB Search でクエリを検索、追跡する方法の概要については、こちらのビデオをご覧ください。このビデオでは、 MongoDB Search 演算子 と、 MongoDB Search が結果内のドキュメントを スコア付けする 方法について詳しく説明しています。

所要時間: 15 分

次のクライアントを使用して、 MongoDB Search クエリを作成および実行できます。

mongot はエラーを返しませんが、 $searchクエリが次の場合には空の結果セットを返します。

  • 存在しないインデックスを参照します。クエリでインデックス名を指定しない場合、 MongoDB Search はデフォルトで default という名前のインデックスを使用します。default という名前のインデックスがない場合、または指定したインデックスが存在しない場合、 MongoDB Search はエラーを返さず、空の結果セットを返します。index オプションを使用して、名前で有効なインデックスを指定できます。

  • インデックスのないフィールドを指定します。インデックスがないフィールドに対してクエリを実行した場合、 MongoDB Search はエラーを返さず、空の結果セットを返します。path パラメータの値としてインデックス付きフィールドのみを指定する必要があります。コレクションのインデックス定義で 動的マッピング を有効にすると、コレクション内のすべての動的にインデックス作成可能なフィールドが自動的にインデックス化されます。詳しくは、動的マッピングを参照してください。

  • Uses the text operator on a field path which is not indexed as a string type. If a field is indexed as a MongoDB Search field type other than string, such as stringFacet or autocomplete, MongoDB Search doesn't return an error and returns an empty result set. You must index the fields with string BSON data type values as string type to query the fields using the text operator.

mongot は、 $searchクエリが次の場合にPlanExecutorエラーを返します。

  • 誤ったデータ型としてインデックス付けされたフィールドを指定します。この場合、 クエリを実行すると、 MongoDB Search は、誤ってインデックス作成されたフィールドとその正しいデータ型 を識別するエラー メッセージを返します。(例: )。

    PlanExecutor error during aggregation :: caused by :: Cannot facet on field "genres" because
    it was not indexed as a "stringFacet" field.

    例、 facetstringnumber、 、またはdate フィールドに対して (MongoDB Search 演算子)クエリを実行するには、stringFacetnumber 、 などの対応するMongoDB Searchフィールドタイプを使用してフィールドのインデックスを作成します。それぞれ とdate です。詳細については、サポートされているデータ型とサポートされていないデータ型 を参照してください。