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

db. コレクション.explain()(mongoshメソッド)

MongoDB とドライバー

This page documents a mongosh method. To see the equivalent method in a MongoDB driver, see the corresponding page for your programming language:
db.collection.explain()

次のメソッドのクエリ プランに関する情報を返します。

mapReduce()に関する情報を返します。

To use db.collection.explain(), append one of the aforementioned methods to db.collection.explain():

db.collection.explain().<method(...)>

たとえば、

db.products.explain().remove( { category: "apparel" }, { justOne: true } )

その他の例については、「」を参照してください。 db.collection.explain().help() も参照してください。

The db.collection.explain() method has the following parameter:

Parameter
タイプ
説明

verbosity

string

任意。 explain 出力の冗長モードを指定します。 モードは explain() の動作に影響し、返される情報の量を決定します。 使用可能なモードは次のとおりです。

  • "queryPlanner" (デフォルト)

  • "executionStats"

  • "allPlansExecution"

以前のバージョンの cursor.explain() との下位互換性のため、MongoDB は true"allPlansExecution" として、false"queryPlanner" として解釈します。

このモードについて詳しくは、「冗長モード」を参照してください。

注意

db.collection.explain() explainコマンドはデフォルトでallPlansExecutionになりますが、このコマンドはデフォルトでqueryPlannerになります。

このメソッドは、次の環境でホストされている配置で使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです

注意

このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

explain を使用するには、基礎のコマンドを実行する権限が必要です。

注意

explainを使用すると、既存のすべてのプラン キャッシュ エントリが無視され、MongoDB クエリ プランナーが新しいプラン キャッシュ エントリを作成できなくなります。

The behavior of db.collection.explain() and the amount of information returned depend on the verbosity mode.

By default, db.collection.explain() runs in queryPlanner verbosity mode.

MongoDB runs the query optimizer to choose the winning plan for the operation under evaluation. db.collection.explain() returns the queryPlanner information for the evaluated method.

MongoDB では、クエリ オプティマイザーが実行され、勝利プランが選択され、勝利プランが完了まで実行され、勝利プランの実行を説明する統計が返されます。

For write operations, db.collection.explain() returns information about the update or delete operations that would be performed, but does not apply the modifications to the database.

db.collection.explain() returns the queryPlanner and executionStats information for the evaluated method. However, executionStats does not provide query execution information for the rejected plans.

また、クエリ オプティマイザーが実行されて最適なプランが選択され、そのプランは完了まで実行されます。"allPlansExecution" モードでは、MongoDBにより、勝利したプランの実行を説明する統計情報と、プランの選択中に取得された他の候補プランの統計情報が返されます。

For write operations, db.collection.explain() returns information about the update or delete operations that would be performed, but does not apply the modifications to the database.

db.collection.explain() returns the queryPlanner and executionStats information for the evaluated method. The executionStats includes the completed query execution information for the winning plan.

クエリオプティマイザが複数のプランを考慮した場合、executionStats 情報には、選択された候補プランと拒否された候補プランの両方について、 プラン選択フェーズ中にキャプチャされ た部分的な実行情報も含まれます。

For write operations, db.collection.explain() returns information about the write operation that would be performed but does not actually modify the database.

You cannot run the explain command/db.collection.explain() in executionStats mode or allPlansExecution mode for an aggregation pipeline that contains the $out stage. Instead, you can either:

  • queryPlanner モードで explain を実行するか、

  • executionStats モード、または allPlansExecution モードで、$out ステージなしでexplain を実行すると、$out ステージに先行するステージの情報が返されます。

The db.collection.explain() method wraps the explain command and is the preferred way to run explain.

db.collection.explain().find() is similar to db.collection.find().explain() with the following key differences:

db.collection.explain().aggregate() is equivalent to passing the explain option to the db.collection.aggregate() method.

To see the list of operations supported by db.collection.explain(), run:

db.collection.explain().help()

db.collection.explain().find() returns a cursor, which allows for the chaining of query modifiers. To see the list of query modifiers supported by db.collection.explain().find() as well as cursor-related methods, run:

db.collection.explain().find().help()

You can chain multiple modifiers to db.collection.explain().find(). For an example, see Explain find() with Modifiers.

バージョン8.3の新機能。

シャーディングされたクラスターに存在しないデータベースに対して db.collection.explain() を実行すると、実行ステージはストリームの終端に達し、操作データベースは作成されません。ストリームの終端の実行統計の詳細については、explain.executionStats.executionStages.isEOF を参照してください。

db.collection.explain() 操作は、次の情報を返す場合があります。

  • explainVersion、出力形式のバージョン("1" など)。

  • command、説明されているコマンドの詳細が表示されます。

  • queryShapeHash は、MongoDB 8.0 以降、クエリシェイプのハッシュを持つ 16 進数文字列です。詳細については、「クエリシェイプ」「クエリシェイプハッシュ」および explain.queryShapeHash を参照してください。

  • queryPlanner は、クエリオプティマイザによって選択されたプランの詳細を示し、拒否されたプランを一覧で表示します。

  • executionStats、当選したプランと拒否されたプランの実行の詳細が表示されます。

  • serverInfo、MongoDB インスタンスに関する情報を提供します。

  • serverParameters内部パラメータの詳細が表示されます。

冗長モード(つまり、queryPlannerexecutionStatsallPlansExecution)は、結果にexecutionStatsが含まれるかどうか、およびexecutionStatsプラン選択中にキャプチャされたデータが含まれるかどうかを決定します。

Explain の出力は、 BSON ドキュメントの最大ネスト深度(100 レベルのネスト)によって制限されます。制限を超える出力は切り捨てられます。

出力の詳細については explain の結果を参照してください。

By default, db.collection.explain() runs in "queryPlanner" verbosity mode.

The following example runs db.collection.explain() in "queryPlanner" verbosity mode to return the query planning information for the specified count() operation:

db.products.explain().count( { quantity: { $gt: 50 } } )

The following example runs db.collection.explain() in "executionStats" verbosity mode to return the query planning and execution information for the specified find() operation:

db.products.explain("executionStats").find(
{ quantity: { $gt: 50 }, category: "apparel" }
)

The following example runs db.collection.explain() in "allPlansExecution" verbosity mode. db.collection.explain() returns the queryPlanner and executionStats for all considered plans for the specified findAndModify() operation:

注意

この explain を実行してもデータは変更されませんが、アップデート操作のクエリ述語が実行されます。候補プランの場合、MongoDB ではプラン選択フェーズ中に取得された実行情報が返されます。

db.products.explain( "allPlansExecution" ).findAndModify( {
query: { name: "Tom", state: "active", rating: { $gt: 10 } },
sort: { rating: 1 },
update: { $inc: { score: 1 } }
} )

db.collection.explain().find() construct allows for the chaining of query modifiers. For example, the following operation provides information on the find() method with sort() and hint() query modifiers.

db.products.explain("executionStats").find(
{ quantity: { $gt: 50 }, category: "apparel" }
).sort( { quantity: -1 } ).hint( { category: 1, quantity: -1 } )

For a list of query modifiers available, run the following in mongosh:

db.collection.explain().find().help()

The db.collection.explain().find() returns the explain() results. If run interactively in mongosh you can use the .finish() method to access the query plan:

db.products.explain().find( { category: "apparel" } ).finish().queryPlanner.winningPlan;