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

MongoDB\Collection::explain()

バージョン1.4の新機能。

MongoDB\Collection::explain()

指定されたコマンドを説明します。

function explain(
MongoDB\Operation\Explainable $explainable,
array $options = []
): array|object
$explainable : MongoDB\Operation\Explainable
explain をするコマンド。
$options : 配列

必要なオプションを指定する配列。

名前
タイプ
説明

comment

混合

データベースプロファイラcurrentOp出力、およびログから操作を追跡するのに役立つ任意のコメントを指定できるようにします。

このオプションは MongoDB 4.4 以降で使用可能であり、古いサーバー バージョンで指定すると実行時に例外が発生します。

デフォルトは説明操作のcomment (存在する場合)。

バージョン1.13の新機能。

readPreference

操作に使用する読み取り設定。 コレクションの読み込み設定(read preference)がデフォルトで設定されます。

typeMap

配列

The type map to apply to cursors, which determines how BSON documents are converted to PHP values. Defaults to the collection's type map.

これは返されたコマンドの結果ドキュメントに使用されます。

冗長

string

コマンドを実行する冗長レベル。 詳細については、 explainコマンドを参照してください。

An array or object with the result document of the explain command. The return type will depend on the typeMap option.

MongoDB\Exception\UnsupportedExceptionオプションが使用され、選択したサーバーでサポートされていない場合(例: collationreadConcernwriteConcern )。

MongoDB\Exception\InvalidArgumentException は、パラメータまたはオプションの解析に関連するエラーの場合は です。

MongoDB$Driver\Exception\RuntimeException は、拡張レベルの他のエラーの場合(例:)。

説明可能なコマンドには、次のものが含まれますが、これらに限定されません。

  • MongoDB\Operation\Aggregate

  • MongoDB\Operation\Count

  • MongoDB\Operation\DeleteMany

  • MongoDB\Operation\DeleteOne

  • MongoDB\Operation\Distinct

  • MongoDB\Operation\Find

  • MongoDB\Operation\FindOne

  • MongoDB\Operation\FindOneAndDelete

  • MongoDB\Operation\FindOneAndReplace

  • MongoDB\Operation\FindOneAndUpdate

  • MongoDB\Operation\UpdateMany

  • MongoDB\Operation\UpdateOne

この例では、カウントコマンドについて説明しています。

<?php
$collection = (new MongoDB\Client)->test->restaurants;
$count = new MongoDB\Operation\Count(
$collection->getDatabaseName(),
$collection->getCollectionName(),
['cuisine' => 'Italian']
);
$result = $collection->explain($count);
var_dump($result);

出力は次のようになります。

object(MongoDB\Model\BSONDocument)#29 (1) {
["storage":"ArrayObject":private]=>
array(4) {
["queryPlanner"]=>
object(MongoDB\Model\BSONDocument)#21 (1) {
["storage":"ArrayObject":private]=>
array(6) {
["plannerVersion"]=>
int(1)
["namespace"]=>
string(16) "test.restaurants"
["indexFilterSet"]=>
bool(false)
["parsedQuery"]=>
object(MongoDB\Model\BSONDocument)#15 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["cuisine"]=>
object(MongoDB\Model\BSONDocument)#14 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["$eq"]=>
string(7) "Italian"
}
}
}
}
["winningPlan"]=>
object(MongoDB\Model\BSONDocument)#19 (1) {
["storage":"ArrayObject":private]=>
array(2) {
["stage"]=>
string(5) "COUNT"
["inputStage"]=>
object(MongoDB\Model\BSONDocument)#18 (1) {
["storage":"ArrayObject":private]=>
array(3) {
["stage"]=>
string(8) "COLLSCAN"
["filter"]=>
object(MongoDB\Model\BSONDocument)#17 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["cuisine"]=>
object(MongoDB\Model\BSONDocument)#16 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["$eq"]=>
string(7) "Italian"
}
}
}
}
["direction"]=>
string(7) "forward"
}
}
}
}
["rejectedPlans"]=>
object(MongoDB\Model\BSONArray)#20 (1) {
["storage":"ArrayObject":private]=>
array(0) {
}
}
}
}
["executionStats"]=>
object(MongoDB\Model\BSONDocument)#27 (1) {
["storage":"ArrayObject":private]=>
array(7) {
["executionSuccess"]=>
bool(true)
["nReturned"]=>
int(0)
["executionTimeMillis"]=>
int(24)
["totalKeysExamined"]=>
int(0)
["totalDocsExamined"]=>
int(25359)
["executionStages"]=>
object(MongoDB\Model\BSONDocument)#25 (1) {
["storage":"ArrayObject":private]=>
array(14) {
["stage"]=>
string(5) "COUNT"
["nReturned"]=>
int(0)
["executionTimeMillisEstimate"]=>
int(20)
["works"]=>
int(25361)
["advanced"]=>
int(0)
["needTime"]=>
int(25360)
["needYield"]=>
int(0)
["saveState"]=>
int(198)
["restoreState"]=>
int(198)
["isEOF"]=>
int(1)
["invalidates"]=>
int(0)
["nCounted"]=>
int(1069)
["nSkipped"]=>
int(0)
["inputStage"]=>
object(MongoDB\Model\BSONDocument)#24 (1) {
["storage":"ArrayObject":private]=>
array(14) {
["stage"]=>
string(8) "COLLSCAN"
["filter"]=>
object(MongoDB\Model\BSONDocument)#23 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["cuisine"]=>
object(MongoDB\Model\BSONDocument)#22 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["$eq"]=>
string(7) "Italian"
}
}
}
}
["nReturned"]=>
int(1069)
["executionTimeMillisEstimate"]=>
int(20)
["works"]=>
int(25361)
["advanced"]=>
int(1069)
["needTime"]=>
int(24291)
["needYield"]=>
int(0)
["saveState"]=>
int(198)
["restoreState"]=>
int(198)
["isEOF"]=>
int(1)
["invalidates"]=>
int(0)
["direction"]=>
string(7) "forward"
["docsExamined"]=>
int(25359)
}
}
}
}
["allPlansExecution"]=>
object(MongoDB\Model\BSONArray)#26 (1) {
["storage":"ArrayObject":private]=>
array(0) {
}
}
}
}
["serverInfo"]=>
object(MongoDB\Model\BSONDocument)#28 (1) {
["storage":"ArrayObject":private]=>
array(4) {
["host"]=>
string(9) "localhost"
["port"]=>
int(27017)
["version"]=>
string(5) "3.6.1"
["gitVersion"]=>
string(40) "025d4f4fe61efd1fb6f0005be20cb45a004093d1"
}
}
["ok"]=>
float(1)
}
}
  • explainコマンドリファレンス (MongoDB マニュアルの参照)